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
  • By using Arduino write a code that connects two LEDs to two push-buttons. Each button controls...

    By using Arduino write a code that connects two LEDs to two push-buttons. Each button controls the state of its connected LED. The LEDs can be in one of two states: Blink-mode or Off-mode. When a button-press event is detected, its connected LED will begin blinking (turning on and off) and when a button-press is detected, its connected LED will turn off. The application should blink one of the LEDs at a rate of 1 second, and the other LED...

  • C CODE LANGUAGE. MSP430. #include <msp430fr6989.h> #define redLED BIT0 // Red LED at P1.0 #define greenLED...

    C CODE LANGUAGE. MSP430. #include <msp430fr6989.h> #define redLED BIT0 // Red LED at P1.0 #define greenLED BIT7 // Green LED at P9.7 #define BUT1 BIT1 // Button S1 at P1.1 #define BUT2 BIT2 // Button S2 at P1.2 void main(void) { WDTCTL = WDTPW | WDTHOLD; // Stop the Watchdog timer PM5CTL0 &= ~LOCKLPM5; // Enable the GPIO pins // Configure and initialize LEDs P1DIR |= redLED; // Direct pin as output P9DIR |= greenLED; // Direct pin as output...

  • Microcontroller programming exercises by practice on Arduino: Write a program that lights a green LED attached...

    Microcontroller programming exercises by practice on Arduino: Write a program that lights a green LED attached to pin 3. The green LED should turn off after a button attached to pin4 has been pushed and released 3 times. Each time the button is pushed it will turn on a red LED attached to pin 7 and turn off a yellow LED attached to pin 9. When the button is not pushed the red LED will be off and the yellow...

  • Write a program in CircuitPython to run on the FM4E that will turn on the heater...

    Write a program in CircuitPython to run on the FM4E that will turn on the heater for one minute if the button is pressed. The LED should be turned on when the heater is on, and it should go off when the heater turns off after one minute. Make sure your code has comments.

  • A button is connected to p1.0 and an LED is connected to P2.0. Write code with PWM so that if the...

    A button is connected to p1.0 and an LED is connected to P2.0. Write code with PWM so that if the button is pressed the LED is on 70%, but if the button is not pressed the LED is on 10%

  • Write an Arduino code to read the current temperature using the LM35 temperature sensor. The code...

    Write an Arduino code to read the current temperature using the LM35 temperature sensor. The code should include the following if the temperature is more than 45 "C: • Turn ON red LED, and after 1 second turn ON the yellow LED, and after another 2 seconds tum On the Green LED. One second later the three LEDS should start blinking (all LEDs OFF for 0.5 seconds, then all LEDS ON for 1 minute), and turn OFF all LEDs after...

  • Design a circuit that safely turns an LED on when the lights in the room turn...

    Design a circuit that safely turns an LED on when the lights in the room turn OFF. The LED should have a nearly constant output(current) while illuminated. Must use a single LED, a single 741 op amp and a light dependent resistor. Resistor value can be vary.

  • Connect NodeMCU to Arduino, write codes on Arduino to make the LED on NodeMCU do the...

    Connect NodeMCU to Arduino, write codes on Arduino to make the LED on NodeMCU do the followings: Using your NodeMCU, write a program to blink the onboard LED in a particular pattern: First, turn the LED on for one second, then off for one second. Then, blink the LED five times – on for 200ms, and off for 200ms. (Use a for loop.) Finally, turn the LED on for half a second, then off for half a second. Loop this...

  • Questions 3 Write a Python program that implements the following functionality: 1. Makes an LED blink....

    Questions 3 Write a Python program that implements the following functionality: 1. Makes an LED blink. 2. Then, on a push of a button the blinking stops. 3. Then, on another push of a button the blinking starts again 4. The behavior of steps 2 and 3 keeps repeating whenever the button is pushed. In writing your program, you should use a class, call it Blinkclass, that helps in making the LED blink or not. Connect the needed circuit using...

  • 4. In this problem you will use CodeWarrior to develop the code to implement a three-light...

    4. In this problem you will use CodeWarrior to develop the code to implement a three-light traffic signal. The program should do the following: a. Turn on a green LED attached to Port B bit 2 for 60 seconds, b. Turn off the green LED and turn on a yellow LED attached to Port B bit 1 for 30 seconds, c. Turn off the yellow LED and turn on a red LED attached to Port B bit 0 for 60...

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