4

I have a raspberry pi zero and I am trying to change the gpio pin values. I tried using RPi.GPIO with python and the gpio command from wiringpi but I am unable to change the value for any of the pins. I am using LEDs to check if a pin is high or low and no matter I do, the pins do not become high. I also used gpio readall to see the changes. I am able to change the mode of the pin (input or output) but not the value itself.

Interestingly an LED connected to pin 26 (BCM numbering) is always on. I tried changing the values but the LED stays on and gpio readall shows its value as 0.

I tried reinstalling the OS but after that twice, the problem still persists. I have raspbian jessie lite.

I have been trying to fix this issue for the last two days, any help would be greatly appreciated.

EDIT: Here is the code I used. No errors were reported when running any of the commands or programs

Python

import RPi.GPIO as gp
gp.setmode(gp.BCM)

gp.setup(18,gp.OUT)

print "before on"
gp.output(18,gp.HIGH)
print "after on"

wiringpi

Output of gpio readall before changing state

+-----+-----+---------+------+---+-Pi Zero--+---+------+---------+-----+-----+
| BCM | wPi |   Name  | Mode | V | Physical | V | Mode | Name    | wPi | BCM |
+-----+-----+---------+------+---+----++----+---+------+---------+-----+-----+
|     |     |    3.3v |      |   |  1 || 2  |   |      | 5v      |     |     |
|   2 |   8 |   SDA.1 |   IN | 0 |  3 || 4  |   |      | 5v      |     |     |
|   3 |   9 |   SCL.1 |   IN | 0 |  5 || 6  |   |      | 0v      |     |     |
|   4 |   7 | GPIO. 7 |   IN | 0 |  7 || 8  | 0 | ALT0 | TxD     | 15  | 14  |
|     |     |      0v |      |   |  9 || 10 | 0 | ALT0 | RxD     | 16  | 15  |
|  17 |   0 | GPIO. 0 |   IN | 0 | 11 || 12 | 0 | OUT  | GPIO. 1 | 1   | 18  |
|  27 |   2 | GPIO. 2 |   IN | 0 | 13 || 14 |   |      | 0v      |     |     |
|  22 |   3 | GPIO. 3 |   IN | 0 | 15 || 16 | 0 | OUT  | GPIO. 4 | 4   | 23  |
|     |     |    3.3v |      |   | 17 || 18 | 0 | IN   | GPIO. 5 | 5   | 24  |
|  10 |  12 |    MOSI |   IN | 0 | 19 || 20 |   |      | 0v      |     |     |
|   9 |  13 |    MISO |   IN | 0 | 21 || 22 | 0 | IN   | GPIO. 6 | 6   | 25  |
|  11 |  14 |    SCLK |   IN | 0 | 23 || 24 | 0 | IN   | CE0     | 10  | 8   |
|     |     |      0v |      |   | 25 || 26 | 0 | IN   | CE1     | 11  | 7   |
|   0 |  30 |   SDA.0 |   IN | 0 | 27 || 28 | 0 | IN   | SCL.0   | 31  | 1   |
|   5 |  21 | GPIO.21 |   IN | 0 | 29 || 30 |   |      | 0v      |     |     |
|   6 |  22 | GPIO.22 |   IN | 0 | 31 || 32 | 0 | IN   | GPIO.26 | 26  | 12  |
|  13 |  23 | GPIO.23 |   IN | 0 | 33 || 34 |   |      | 0v      |     |     |
|  19 |  24 | GPIO.24 |   IN | 0 | 35 || 36 | 0 | IN   | GPIO.27 | 27  | 16  |
|  26 |  25 | GPIO.25 |   IN | 0 | 37 || 38 | 0 | 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 Zero--+---+------+---------+-----+-----+

I ran the following commands

gpio -g mode 26 out
gpio -g write 26 1

This is the output of gpio readall after attempting to change the value

 +-----+-----+---------+------+---+-Pi Zero--+---+------+---------+-----+-----+
| BCM | wPi |   Name  | Mode | V | Physical | V | Mode | Name    | wPi | BCM |
+-----+-----+---------+------+---+----++----+---+------+---------+-----+-----+
|     |     |    3.3v |      |   |  1 || 2  |   |      | 5v      |     |     |
|   2 |   8 |   SDA.1 |   IN | 0 |  3 || 4  |   |      | 5v      |     |     |
|   3 |   9 |   SCL.1 |   IN | 0 |  5 || 6  |   |      | 0v      |     |     |
|   4 |   7 | GPIO. 7 |   IN | 0 |  7 || 8  | 0 | ALT0 | TxD     | 15  | 14  |
|     |     |      0v |      |   |  9 || 10 | 0 | ALT0 | RxD     | 16  | 15  |
|  17 |   0 | GPIO. 0 |   IN | 0 | 11 || 12 | 0 | OUT  | GPIO. 1 | 1   | 18  |
|  27 |   2 | GPIO. 2 |   IN | 0 | 13 || 14 |   |      | 0v      |     |     |
|  22 |   3 | GPIO. 3 |   IN | 0 | 15 || 16 | 0 | OUT  | GPIO. 4 | 4   | 23  |
|     |     |    3.3v |      |   | 17 || 18 | 0 | IN   | GPIO. 5 | 5   | 24  |
|  10 |  12 |    MOSI |   IN | 0 | 19 || 20 |   |      | 0v      |     |     |
|   9 |  13 |    MISO |   IN | 0 | 21 || 22 | 0 | IN   | GPIO. 6 | 6   | 25  |
|  11 |  14 |    SCLK |   IN | 0 | 23 || 24 | 0 | IN   | CE0     | 10  | 8   |
|     |     |      0v |      |   | 25 || 26 | 0 | IN   | CE1     | 11  | 7   |
|   0 |  30 |   SDA.0 |   IN | 0 | 27 || 28 | 0 | IN   | SCL.0   | 31  | 1   |
|   5 |  21 | GPIO.21 |   IN | 0 | 29 || 30 |   |      | 0v      |     |     |
|   6 |  22 | GPIO.22 |   IN | 0 | 31 || 32 | 0 | IN   | GPIO.26 | 26  | 12  |
|  13 |  23 | GPIO.23 |   IN | 0 | 33 || 34 |   |      | 0v      |     |     |
|  19 |  24 | GPIO.24 |   IN | 0 | 35 || 36 | 0 | IN   | GPIO.27 | 27  | 16  |
|  26 |  25 | GPIO.25 |  OUT | 0 | 37 || 38 | 0 | 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 Zero--+---+------+---------+-----+-----+

gpio -g read 26 returns a value of 0. gpio -g mode 18 out and gpio -g write 18 1 also had no effect

EDIT 2 in case anyone was wondering, the LEDs work fine. I verified that by connecting the LEDs to a different battery powered circuit.

I also changed the circuit a bit and the LED connected to pin 26 no longer lights up. the particular LED was not changed but I made my circuit similar to that of one of my earlier project here. The required LEDs still do not light up. This was the output of my testing. The circuit is connected properly.

pi@raspberrypi:~/repo/lightswitch $ ./lightswitch 
LED is off
Turning on LED
pi@raspberrypi:~/repo/lightswitch $ ./lightswitch 
LED is off
Turning on LED

EDIT 3 So it seems that pin 6 has the same always on state that pin 26 used to have. Interestingly, gpio readall shows it as input and as having a value of 0.

0

2 Answers 2

2

It seems to me that the problem is wiring. You don't show it in this question, but the image in the link you provide is this:

Don't use this:

bad wiring

That's not going to work reliably if it works at all. First, the LED needs to have a current-limiting resistor. A value of 330 ohms should work for most LEDs and give sufficient brightness (around 10mA current).

Second, you are apparently connecting pin 12 (GPIO18) and pin 38 (GPIO20) directly together. This is almost undoubtedly an error.

Use this:

Instead, try this:

Pin 6 (Ground) ----/\/\/-----|<|----- Pin 38 (GPIO20)
                   330 R     LED

Then try this from a command line (bash):

# choose GPIO20
sudo echo "20" /sys/class/gpio/export
# make it an output pin
sudo echo "out" /sys/class/gpio/gpio20/direction
# turn the LED on
sudo echo "1" /sys/class/gpio/gpio20/value
# turn the LED off
sudo echo "0" /sys/class/gpio/gpio20/value

Note that the cathode of the LED (shorter leg, also facing flat side of LED) must be connected toward the ground side and the anode (longer leg) toward the GPIO pin.

Once all of that works, you can proceed to using WiringPi or C or whatever you use to control pins. To use your Python example, you could use this:

import RPi.GPIO as gp
gp.setmode(gp.BCM)
gp.setup(20,gp.OUT)
gp.output(20, 1)
print "LED should now be on"
1

This is just an idea. If you already have wiringPi you could try using C. Try something like this:

#include <stdio.h>
#include <wiringPi.h>
const int modePin = 18; 
int main(void){
    wiringPiSetupGpio(); //initialize wiringPi
    pinMode(modePin, OUTPUT);
    digitalWrite(modePin, HIGH); 
    return 0;
}

Note I did not test this code.

1
  • Thanks unfortunately I'm out of state right now so I won't be able to test the code for a few weeks but I'll try it and let you know how it goes. Also I don't know C so I'm hoping your works
    – Rumesh
    Commented Jun 9, 2017 at 4:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.