0

I have two switches (buttons). Red is connected to physical pin 38 (BCM 20) and green to physical pin 40 (BCM 21). I call wiringPiSetupGPIO() it returns 0. When I use

pullUpDnControl(redSwitch, PUD_UP);   // redSwitch = 20
pullUpDnControl(greenSwitch,PUD_UP); // greenSwitch = 21;

and then print

digitalRead(redSwitch), digitalRead(greenSwitch)

red switch is always 0. The greenSwitch prints, 0 when it is pressed, 1 otherwise.

I could have sworn I had this working on a Raspberry Pi at home. It might be a problem with the ribbon, but does anyone have another idea as to why this might be misbehaving?

As per a request, the code is below. I am running jumpers directly out of a ribbon and then closing them for testing purposes, but my output is always:

red: 0, green: 1

unless I connect pin 21 to ground, when I get:

red: 0, green: 0

red should be high (1) to start, but it is not.

Note that if I switch to pin 7, redSwitch works as advertised ... what's so special about pin 20?

const int redLED = 14;
const int yellowLED = 15;
const int greenLED = 18;
const int blueLED = 24; // 23 didn't work

const int redSwitch = 20; // this doesn't work
const int greenSwitch = 21; // this does

const int dht22 = 16;
const int buzzer = 25;

const int loopDelay = 125; // to avoid reporting multiple switch pushes
const int note = 880;

void setup();
void loop();
void switchDetection();
void flashAll();

void setup(){
    int status = wiringPiSetupGpio();
    printf("Status is %d\n",status);

    pullUpDnControl(redSwitch, PUD_UP);
    pullUpDnControl(greenSwitch, PUD_UP);   

    status = softToneCreate(buzzer);    
    printf("Soft tone create status: %d\n",status);

}

void loop(){

    while(1){
        printf("red: %d, green: %d\n",digitalRead(redSwitch),digitalRead(greenSwitch));
    }
}
4
  • Edit your post with your complete code in a code block and an image of your wiring.
    – CoderMike
    Commented Dec 7, 2018 at 17:03
  • Are you getting the pin numbers right? pinout.xyz/pinout/pin40_gpio21
    – Dougie
    Commented Dec 7, 2018 at 17:10
  • That might be my problem, but I have const int redSwitch = 20; const int greenSwitch = 21; Commented Dec 7, 2018 at 17:14
  • What version of the WiringPi library do you have? You need v2.46 for 3B+ & all earlier Raspberries. V2.46 doesn't work on a 3A+.
    – Dougie
    Commented Dec 8, 2018 at 10:06

1 Answer 1

1

Thanks everyone for your responses, I really appreciate them! I downloaded pigpio and also consulted with the author of WiringPi, and it turns out that I had fried several GPIO pins (no idea how: until yesterday I did not even know that was a possibility), including pin # 20.

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.