Question

write a code in C on RaspberryPi to have an LED light turn on and off...

write a code in C on RaspberryPi to have an LED light turn on and off using a push button

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Answer :

please upvote if its upto your expectation or comment for any query . Thanks

I am writing code for Raspberry pi 3 .

Hardware connection :

1. LED 3.3v connected to GPIO 2 which is BCM2 (SDA ) with 100 ohm resistor .

2. PUSH button 1 connected to GPIO 3 which is SCL pin . (for turn on led)

3. PUSH button 2   connected to GPIO17 to turn off led

Program Plan :

1. set GPIO2 led pin as OUTPUT .

2. set GPIO3 push button pin as INPUT and set pullup resistor high to detect low on push button pin .

3. run a while loop where we will check continuously push button.

4. in if statement will check for push button pressed if pressed we will get 0 of pin status and will set LED high .

5. if second button pressed turn off led.

Code :

#include <wiringPi.h>           //hardware  definition file
#include <stdio.h>

#define LED    2    //GPIO 2
#define OnButton 3      //GPIO 3   to turn on led 
#define OffButton 17   //GPIO 17  to turn off led

int main(void)
{

    
    pinMode(LED, OUTPUT);   //set o/p to led pin 
    pinMode(OnButton, INPUT);  //set input on button pin 
    pinMode(OffButton, INPUT);  //set input on button pin 

    pullUpDnControl(OnButton, PUD_UP);             //pull up set to detect low on pin 
    pullUpDnControl(OffButton, PUD_UP);             //pull up set to detect low on pin 
    while(1){ 
    
        if(digitalRead(OnButton) == 0){ //button presses 
            digitalWrite(LED, HIGH);   //Set LED high 
           delay(100); //debounce time 
          
        }
        else if(digitalRead(OffButton)==0){
             digitalWrite(LED,LOW);  //turn off led
             delay(100); //debounce time 
        }
    }

    return 0;
}
Add a comment
Know the answer?
Add Answer to:
write a code in C on RaspberryPi to have an LED light turn on and off...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT