"Spare" pins on the GPIO connector

A place to discuss any hardware issues you are having with your CrowPi
wizzy
Posts: 16
Joined: Wed Nov 04, 2020 5:43 pm

"Spare" pins on the GPIO connector

Post by wizzy »

I went though all the GPIO pins documented on the Crowpi2 sensor board - I am looking to see what pins I can use for a rotary encoder, but still use other sensors. Maybe this is the same for the CrowPi ?

The following GPIO pins appear to be unused ?

UART - GPIO 14, 15 (on black connector at top of board)

GPIO5
GPIO 12, 13, 18 (all PWM capable)
GPIO 16, 17

There are no true interrupts on the RPi ?
(On my Arduino, I used interrupts for the rotary encoder)
wizzy
Posts: 16
Joined: Wed Nov 04, 2020 5:43 pm

Re: "Spare" pins on the GPIO connector

Post by wizzy »

The Crowpi2 legend on the RPi pins is difficult to understand.

My rotary encoder is 5V, so I used a 10k + 22k resistive divider for a level translator.

Because it now has about 8k output resistance, I just tried it on all the pins
while watching the signal to see which I could use as an input ..

I think the UART pins would have level translators - would these be smart, bidirectional ones ?

I found BCM pins 10, 9, 16, 1, 15 to be
high impedance - but I tickled the relay and the buzzer during the tests, and maybe affected other things.
2600Sam
Posts: 27
Joined: Thu Jul 30, 2020 6:04 am

Re: "Spare" pins on the GPIO connector

Post by 2600Sam »

These are for 3.3V rotary encoder but should work for 5V just change the power pin from pin 1 to 2 or 4

Rotary encoder:
Rotary_RPI.png
Rotary_RPI.png (98.49 KiB) Viewed 4387 times
python code:

Code: Select all

import RPi.GPIO as GPIO
from time import sleep
 
counter = 10
 
Enc_A = 17  #  pin 11
Enc_B = 27  #  pin 13
 
 
def init():
    print "Rotary Encoder Test Program"
    GPIO.setwarnings(True)
    GPIO.setmode(GPIO.BCM)
    GPIO.setup(Enc_A, GPIO.IN)
    GPIO.setup(Enc_B, GPIO.IN)
    GPIO.add_event_detect(Enc_A, GPIO.RISING, callback=rotation_decode, bouncetime=10)
    return
 
 
def rotation_decode(Enc_A):
    global counter
    sleep(0.002)
    Switch_A = GPIO.input(Enc_A)
    Switch_B = GPIO.input(Enc_B)
 
    if (Switch_A == 1) and (Switch_B == 0):
        counter += 1
        print "direction -> ", counter
        while Switch_B == 0:
            Switch_B = GPIO.input(Enc_B)
        while Switch_B == 1:
            Switch_B = GPIO.input(Enc_B)
        return
 
    elif (Switch_A == 1) and (Switch_B == 1):
        counter -= 1
        print "direction <- ", counter
        while Switch_A == 1:
            Switch_A = GPIO.input(Enc_A)
        return
    else:
        return
 
def main():
    try:
        init()
        while True :
            sleep(1)
 
    except KeyboardInterrupt:
        GPIO.cleanup()
 
if __name__ == '__main__':
    main()
ALTERNATE
rotary-encoder.jpg
rotary-encoder.jpg (71 KiB) Viewed 4386 times

Code: Select all

from RPi import GPIO
from time import sleep

clk = 17  # pin 11
dt = 18  #  pin 12

GPIO.setmode(GPIO.BCM)
GPIO.setup(clk, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
GPIO.setup(dt, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)

counter = 0
clkLastState = GPIO.input(clk)

try:
        while True:
               clkState = GPIO.input(clk)
               dtState = GPIO.input(dt)
               if clkState != clkLastState:
                       if dtState != clkState:
                               counter += 1
                       else:
                               counter -= 1
                       print counter
               clkLastState = clkState
               sleep(0.01)
finally:
       GPIO.cleanup()

Those should work!
wizzy
Posts: 16
Joined: Wed Nov 04, 2020 5:43 pm

Re: "Spare" pins on the GPIO connector

Post by wizzy »

Thanks for your reply.

My encoder, off some old equipment, is 5V. I do not want to drive the RPi pins to 5V, so neither of your diagrams work for me.

I am not going to fritz this, but my solution is 10K from encoder, and then 22K to ground - a voltage divider.
I connect the RPi input to the centre tap.

With that, open circuit, I get a nice square wave on both resistor centre taps of 0 -> 2.7V - great.

In the first one, you are using 17, 27, 22 as inputs - I found the following on the CrowPi2 :-

Pin 17, after connecting to my resistor centre tap, swings 0 -> 0.2V - i.e. pin 17 is being driven.
Pin 27, swings 0 -> 0.7V
Pin 22, swings 1.8V -> 2.9V

In your second diagram, you are using Pins 17, 18 as input. We have covered 17.

Pin 18 - immediately triggers sound output - has about 3khz 0.1V peak-to-peak signal.

Thus all these pins are being used already.

Pins 15 and 16 give me a nice clean 0 - 2.7V swing.

GPIO.gpio_function() on pins 17, 27, 22, 18, 16 all return 1 (GPIO.IN) - so they are driven from the CrowPi2 sensors.

GPIO.gpio_function() on pin 15 returns -1 (GPIO.UNKNOWN).

Thank you for your code snippets - I am using wait_for_edge, but the callback is a better idea.
It all works - I get my encoder value, and can write it to the LED display.

Back to the subject - I need to know which RPi pins are Spare - that I can use as I wish.
2600Sam
Posts: 27
Joined: Thu Jul 30, 2020 6:04 am

Re: "Spare" pins on the GPIO connector

Post by 2600Sam »

You're using the Crowpi2 and can access the GPIO pin without disconnecting the pi from the board (right) on the Crowpi1 we can not?
And your possible the only crowpi2 user here (I might get one later when they start sell them here)!

You can use
GPIO readall
which may fail for "unable to determine board type" (on the Pi4) if it does do

Code: Select all

wget https://project-downloads.drogon.net/wiringpi-latest.deb

Code: Select all

sudo dpkg -i wiringpi-latest.deb
The GPIO readall should now work!
Look for GPIO28 & 29 on the first Pi there was an unused header and you could access GPIO28-31 on the Pi4 GPIO28 & 29 are now routed to the camera connector and are a second i2c (I2C0) maybe required for camera functionality (I don't know) 30 & 31 may have been dropped.

How would you get a value from an encoder, it should just show rotation CW or CCW (up or down - left or right)??
Last edited by 2600Sam on Mon Nov 16, 2020 2:43 am, edited 1 time in total.
2600Sam
Posts: 27
Joined: Thu Jul 30, 2020 6:04 am

Re: "Spare" pins on the GPIO connector

Post by 2600Sam »

You can create interrupts with python

Code: Select all

from RPi import GPIO
from time import sleep

clk = 17
dt = 18  

GPIO.setmode(GPIO.BCM)
GPIO.setup(clk, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(dt, GPIO.IN, pull_up_down=GPIO.PUD_UP)

clkLastState = GPIO.input(clk)

def my_callback(channel):  
    global clkLastState
    global counter
    try:
                clkState = GPIO.input(clk)
                if clkState != clkLastState:
                        dtState = GPIO.input(dt)
                        if dtState != clkState:
                                counter += 1
                        else:
                                counter -= 1
						print counter
                clkLastState = clkState
                #sleep(0.01)
    finally:
                print "Ending"


counter = 0
clkLastState = GPIO.input(clk)
GPIO.add_event_detect(17, GPIO.FALLING  , callback=my_callback, bouncetime=300)  # interruption
raw_input("Enter anything")
GPIO.cleanup()
wizzy
Posts: 16
Joined: Wed Nov 04, 2020 5:43 pm

Re: "Spare" pins on the GPIO connector

Post by wizzy »

using the Crowpi2 and can access the GPIO pin without disconnecting the pi from the board (right)
Yes. Perhaps I was not clear - indeed, I would like to connect elsewhere and still be hooked up to the Crowpi2 sensors.

In particular, I would also like a free "select" to use with one of the SPI channels.
How would you get a value from an encoder
I used code similar to what you provided to increment/decrement a counter, and put it up on the 7 seg display. It works fine (using pins 15 and 16 - or, in wPi nomenclature, RxD and GPIO27)

The relay is on GPIO 29 - and I can hear it clicking if I connect wires to that pin. I have been using BCM numbering in my posts - which makes it Pin 21 - but I have been confusing the two numbering schemes myself. Do we prefer the wPi naming ?

The camera on the CrowPi2 does not connect to the camera connector - it uses a somewhat awkward USB cable that comes out and plugs into the RPi.

I append my "gpio readall" - thanks again for all your help.

Code: Select all

+-----+-----+---------+------+---+---Pi 4B--+---+------+---------+-----+-----+
 | BCM | wPi |   Name  | Mode | V | Physical | V | Mode | Name    | wPi | BCM |
 +-----+-----+---------+------+---+----++----+---+------+---------+-----+-----+
 |     |     |    3.3v |      |   |  1 || 2  |   |      | 5v      |     |     |
 |   2 |   8 |   SDA.1 | ALT0 | 1 |  3 || 4  |   |      | 5v      |     |     |
 |   3 |   9 |   SCL.1 | ALT0 | 1 |  5 || 6  |   |      | 0v      |     |     |
 |   4 |   7 | GPIO. 7 |   IN | 1 |  7 || 8  | 1 | ALT5 | TxD     | 15  | 14  |
 |     |     |      0v |      |   |  9 || 10 | 1 | ALT5 | RxD     | 16  | 15  |
 |  17 |   0 | GPIO. 0 |   IN | 0 | 11 || 12 | 0 | IN   | GPIO. 1 | 1   | 18  |
 |  27 |   2 | GPIO. 2 |   IN | 0 | 13 || 14 |   |      | 0v      |     |     |
 |  22 |   3 | GPIO. 3 |   IN | 1 | 15 || 16 | 0 | IN   | GPIO. 4 | 4   | 23  |
 |     |     |    3.3v |      |   | 17 || 18 | 0 | IN   | GPIO. 5 | 5   | 24  |
 |  10 |  12 |    MOSI | ALT0 | 0 | 19 || 20 |   |      | 0v      |     |     |
 |   9 |  13 |    MISO | ALT0 | 0 | 21 || 22 | 1 | IN   | GPIO. 6 | 6   | 25  |
 |  11 |  14 |    SCLK | ALT0 | 0 | 23 || 24 | 1 | OUT  | CE0     | 10  | 8   |
 |     |     |      0v |      |   | 25 || 26 | 1 | OUT  | CE1     | 11  | 7   |
 |   0 |  30 |   SDA.0 |  OUT | 0 | 27 || 28 | 1 | IN   | SCL.0   | 31  | 1   |
 |   5 |  21 | GPIO.21 |   IN | 1 | 29 || 30 |   |      | 0v      |     |     |
 |   6 |  22 | GPIO.22 |   IN | 1 | 31 || 32 | 0 | IN   | GPIO.26 | 26  | 12  |
 |  13 |  23 | GPIO.23 |   IN | 1 | 33 || 34 |   |      | 0v      |     |     |
 |  19 |  24 | GPIO.24 |   IN | 1 | 35 || 36 | 0 | IN   | GPIO.27 | 27  | 16  |
 |  26 |  25 | GPIO.25 |   IN | 0 | 37 || 38 | 1 | IN   | GPIO.28 | 28  | 20  |
 |     |     |      0v |      |   | 39 || 40 | 0 | IN   | GPIO.29 | 29  | 21  |
 +-----+-----+---------+------+---+----++----+---+------+---------+-----+-----+
 | BCM | wPi |   Name  | Mode | V | Physical | V | Mode | Name    | wPi | BCM |
 +-----+-----+---------+------+---+---Pi 4B--+---+------+---------+-----+-----+
2600Sam
Posts: 27
Joined: Thu Jul 30, 2020 6:04 am

Re: "Spare" pins on the GPIO connector

Post by 2600Sam »

The relay is on GPIO 29 - and I can hear it clicking if I connect wires to that pin. I have been using BCM numbering in my posts - which makes it Pin 21 - but I have been confusing the two numbering schemes myself. Do we prefer the wPi naming ?
Pin21 = GPIO9 not GPIO29
I took a look at the pictures you posted, and they list GPIO28 for the IR and GPIO29 for the relay, interesting.

As for naming use what is easiest for you, I use physical or BCM (although physical is the least confusing and I learned BCM first)

in Python just look for the line
GPIO.setmode(GPIO.BOARD)
clk = 11 # PIN 11
or
GPIO.setmode(GPIO.BCM)
clk = 17 # pin 11(GPIO17)
2600Sam
Posts: 27
Joined: Thu Jul 30, 2020 6:04 am

Re: "Spare" pins on the GPIO connector

Post by 2600Sam »

On the CrowPi1 they use Physical pin numbers printed on the top but most of the python programs use BCM


Here is my CrowPi1 Rpi3 gpio readall

Code: Select all

 +-----+-----+---------+------+---+---Pi 3B+-+---+------+---------+-----+-----+
 | BCM | wPi |   Name  | Mode | V | Physical | V | Mode | Name    | wPi | BCM |
 +-----+-----+---------+------+---+----++----+---+------+---------+-----+-----+
 |     |     |    3.3v |      |   |  1 || 2  |   |      | 5v      |     |     |
 |   2 |   8 |   SDA.1 | ALT0 | 1 |  3 || 4  |   |      | 5v      |     |     |
 |   3 |   9 |   SCL.1 | ALT0 | 1 |  5 || 6  |   |      | 0v      |     |     |
 |   4 |   7 | GPIO. 7 |   IN | 1 |  7 || 8  | 1 | ALT5 | TxD     | 15  | 14  |
 |     |     |      0v |      |   |  9 || 10 | 1 | ALT5 | RxD     | 16  | 15  |
 |  17 |   0 | GPIO. 0 |   IN | 0 | 11 || 12 | 0 | IN   | GPIO. 1 | 1   | 18  |
 |  27 |   2 | GPIO. 2 |   IN | 0 | 13 || 14 |   |      | 0v      |     |     |
 |  22 |   3 | GPIO. 3 |   IN | 1 | 15 || 16 | 0 | IN   | GPIO. 4 | 4   | 23  |
 |     |     |    3.3v |      |   | 17 || 18 | 1 | IN   | GPIO. 5 | 5   | 24  |
 |  10 |  12 |    MOSI | ALT0 | 1 | 19 || 20 |   |      | 0v      |     |     |
 |   9 |  13 |    MISO | ALT0 | 1 | 21 || 22 | 1 | IN   | GPIO. 6 | 6   | 25  |
 |  11 |  14 |    SCLK | ALT0 | 0 | 23 || 24 | 1 | OUT  | CE0     | 10  | 8   |
 |     |     |      0v |      |   | 25 || 26 | 1 | OUT  | CE1     | 11  | 7   |
 |   0 |  30 |   SDA.0 |   IN | 1 | 27 || 28 | 1 | IN   | SCL.0   | 31  | 1   |
 |   5 |  21 | GPIO.21 |   IN | 1 | 29 || 30 |   |      | 0v      |     |     |
 |   6 |  22 | GPIO.22 |   IN | 1 | 31 || 32 | 0 | IN   | GPIO.26 | 26  | 12  |
 |  13 |  23 | GPIO.23 |   IN | 1 | 33 || 34 |   |      | 0v      |     |     |
 |  19 |  24 | GPIO.24 |   IN | 1 | 35 || 36 | 0 | IN   | GPIO.27 | 27  | 16  |
 |  26 |  25 | GPIO.25 |   IN | 1 | 37 || 38 | 1 | IN   | GPIO.28 | 28  | 20  |
 |     |     |      0v |      |   | 39 || 40 | 1 | IN   | GPIO.29 | 29  | 21  |
 +-----+-----+---------+------+---+----++----+---+------+---------+-----+-----+
 | BCM | wPi |   Name  | Mode | V | Physical | V | Mode | Name    | wPi | BCM |
 +-----+-----+---------+------+---+---Pi 3B+-+---+------+---------+-----+-----+
I read somewhere that the wPi (WiringPi) numbers is to make them compatible with Arduino boards.
wizzy
Posts: 16
Joined: Wed Nov 04, 2020 5:43 pm

Re: "Spare" pins on the GPIO connector

Post by wizzy »

You will notice that in my readall , SDA.0 is OUT value 0.

If you set it to 1 for about 10 seconds, it kills the power.

When I do a shutdown, the RPi turns off and a bit later the power to the main board dies.

Side note:- I got a message from Electrow saying
( (They explain I am lucky to have early hardware ) ...

So we need your help to withdraw the comments from the forum, and appease the opinion on public affairs.
Eek. I asked for schematics on the github "issues" feature - I didn't bad-mouth them at all.

But I should edit the comment to say I like the product.

I also asked about a support forum, and linked here ..
Post Reply