2

I am trying to Toggle a led on and off using using a switch connected to a GPIO(pin11). The LED would be connected to another GPIO(pin12).

I used wiringPi's blink program to blink an LED, but i am having difficulty figuring out how to do the next step.

here is my setup

enter image description here

any idea how i should start?

  • As no one has commented, I will put my bit in. What you wan to do is simple, but if we do it you won't learn anything. I am reminded of another programming site "we will not do your homework for you". What have you tried? wiringpi.com has libraries and examples. I suggest you download the MagPi magazines and try some of the suggestions. – Milliways Nov 25 '13 at 7:43
4

Step 1 Take the 3V3 output from the Pi, into the LED, from the LED into the resistor, and from the resistor to the Gnd of the Pi. The LED should light up (no code required). I guess you have already done this step as your LED is on (although I can't quite figure out your wiring).

Step 2 Take GPIO pin 11 output from the Pi, into the LED, from the LED into the resistor, and from the resistor to the Gnd of the Pi. Using RPi.GPIO in python, call GPIO.setmode to select BCM or BOARD, then configure this GPIO pin to be an output, and set the output LOW. The LED should be off. Set the output to HIGH, and the LED should light up.

Step 3 Take GPIO pin 12 from the Pi, into a switch, and from the switch to the Gnd of the Pi. Using RPi.GPIO in python, configure this GPIO pin to be an input and select the internal pull-up resistor. Now look at the GPIO.input of this pin, it should be high. Press the button and look at GPIO.input again, it should be now low (because you've connected it to Gnd).

Step 4 - up to you.

The RPi.GPIO wiki page is an excellent starting point.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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