GPIO Pins Problem


9 posts
by runfastman » Mon Nov 26, 2012 3:27 am
was running some test code and now it doesn't work anymore. I can't set the GPIO pins from high to low with bcm2835_gpio_write

It still works if I go through command line with
sudo -s
sudo echo "17" > /sys/class/gpio/export
sudo echo "out" > /sys/class/gpio/gpio17/direction
cat /sys/class/gpio/gpio17/value
echo "1" > /sys/class/gpio/gpio17/value
echo "0" > /sys/class/gpio/gpio17/value
sudo echo "17" > /sys/class/gpio/unexport

Is there any way to reset the pins somehow? Or is something locking them from my app. Here is a test app I am using to set all of the pins to 1 or 0 and display the value.

#define BCM2708_PERI_BASE 0x20000000
#define GPIO_BASE (BCM2708_PERI_BASE + 0x200000) /* GPIO controller */


#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <dirent.h>
#include <fcntl.h>
#include <assert.h>
#include <unistd.h>
#include <sys/mman.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/time.h>
#include <bcm2835.h>
#include <unistd.h>

int main(int argc, char **argv)
{
if (!bcm2835_init())
return 1;

int allOn = 1;
if (strcmp(argv[1], "ON") == 0) allOn = 1;
if (strcmp(argv[1], "OFF") == 0) allOn = 0;

int pins[17] = {0,1,4,7,8,9,10,11,14,15,17,18,21,22,23,24,25};

int i;
int val;

for (i=0;i<17;i++)
{
if (allOn)
{
printf("Turning on %i\n", pins[i]);
bcm2835_gpio_write(pins[i], HIGH);
if (bcm2835_gpio_lev(pins[i]) == HIGH)
printf("Pin Value = ON\n");
else
printf("Pin Value = OFF\n");
}

else
{
printf("Turning off %i\n", pins[i]);
bcm2835_gpio_write(pins[i], LOW);
if (bcm2835_gpio_lev(pins[i]) == HIGH)
printf("Pin Value = ON\n");
else
printf("Pin Value = OFF\n");
}
delay(1000);
}



}
Posts: 10
Joined: Thu Oct 18, 2012 1:12 am
by joan » Mon Nov 26, 2012 9:13 am
It look OK. What output do you get to the terminal?
User avatar
Posts: 4156
Joined: Thu Jul 05, 2012 5:09 pm
Location: UK
by runfastman » Mon Nov 26, 2012 2:02 pm
Some pins were HIGH and some LOW, but the calls didn't change any of them except pin 14 it was the only one that would change. I will post the exact pin values when I get home.
Posts: 10
Joined: Thu Oct 18, 2012 1:12 am
by phil95 » Mon Nov 26, 2012 3:23 pm
Hello,
I think, your pins are not initialized in output mode.
You can try to add in your program a call to bcm2835_gpio_fsel
for each pin to configuret it to output.
Visibly some pins (14, 16, ...) are already configured to output.
Philippe
Posts: 105
Joined: Wed Sep 12, 2012 8:10 am
Location: Paris
by runfastman » Mon Nov 26, 2012 3:33 pm
I will try that when I get home.
But I noticed it when running the code for this.
http://learn.adafruit.com/dht-humidity- ... cs-logging
It was working and then it stopped. It gets stuck in an infinite loop waiting for the pin to drop.
Posts: 10
Joined: Thu Oct 18, 2012 1:12 am
by Arjan » Mon Nov 26, 2012 5:00 pm
Fyi,
I've found a great util for viewing the gpio_fsel status : http://www.susa.net/wordpress/2012/07/raspberry-pi-gpfsel-gpio-and-pads-status-viewer/
Posts: 128
Joined: Sat Sep 08, 2012 1:59 pm
by runfastman » Tue Nov 27, 2012 2:56 am
OK, I added bcm2835_gpio_fsel(pins[i], BCM2835_GPIO_FSEL_OUTP); to my On/Off code and that made it work.

However the temperature check code I was using still gets stuck in an infinite loop. It uses the bcm2835_gpio_fsel but gets stuck in the "wait for pin to drop" section. It was working a few days ago. :cry:

int readDHT(int type, int pin, float *retTemp) {
int counter = 0;
int laststate = HIGH;
int j=0;

// Set GPIO pin to output
printf("Set GPIO pin to output\n");
bcm2835_gpio_fsel(pin, BCM2835_GPIO_FSEL_OUTP);

bcm2835_gpio_write(pin, HIGH);
usleep(500000); // 500 ms
bcm2835_gpio_write(pin, LOW);
usleep(20000);

bcm2835_gpio_fsel(pin, BCM2835_GPIO_FSEL_INPT);

data[0] = data[1] = data[2] = data[3] = data[4] = 0;

// wait for pin to drop
printf("wait for pin to drop?\n");
while (bcm2835_gpio_lev(pin) == 1) {
usleep(1);
}
Posts: 10
Joined: Thu Oct 18, 2012 1:12 am
by runfastman » Tue Nov 27, 2012 3:30 am
GOT IT. Thanks for the help everyone.
:oops: I had my wiring a little off, so by calling
bcm2835_gpio_fsel(pin, BCM2835_GPIO_FSEL_INPT);
and setting my pin to the input, my pin was reading a 1 from the wire.
Posts: 10
Joined: Thu Oct 18, 2012 1:12 am
by guga » Wed Aug 28, 2013 5:32 pm
HI!

Please help me someone!

I had the same problem, but i cant solve it. I'd like to use DHT22, but the script(adafruit) stuck in while( bcm2835_gpio_lev(pin) == 1 ).

I checked, the sensor get the 0 handshake bit, but i cant receive the handshake of the sensor. But if I touch the data pin of the sensor with an iron ( i realised with multimeter probe ), than the srcipt go out from loop and print the temp and hum.

It is so interesting for me.

Please help, somebody :S
Posts: 1
Joined: Wed Aug 28, 2013 5:25 pm