How to make 2 led blinks pushing a button
I have a raspberry pi, I love electronics so which best example of make 2LEDs blinks using a small pushbutton ?
Requirements
- 1 breadboard
- 1 10kOhm resistor
- 2 3kOhm resistors
- many jumpers
- 1 push button
- 2 LED
- 1 raspberry pi
- 1 plutonium bar (I’m joking )
Software
I wrote a simple C programs to do everything
/*
* How to make 2 leds blinks pushing a button
* By Domenico Luciani aka DLion
*/
//Using wiringPi library
#include <wiringPi.h>
#include <stdio.h>
#include <stdlib.h>
int main (void)
{
//Using pin 8 to read input (pin 3 on the raspi)
int pin8_in = 8;
//Using pin 0 and 1 to write output (pin 11 and 12 on the raspi)
int pin0_out = 0;
int pin1_out = 1;
//Starting Setup
if(wiringPiSetup() == -1)
exit(1);
//Set pin 8 to input
pinMode(pin8_in,INPUT);
//Set pin 0 and 1 to output
pinMode(pin0_out,OUTPUT);
pinMode(pin1_out,OUTPUT);
while(1)
{
//Push button pressed
if(digitalRead(pin8_in) == 0)
{
//LED 0 ON
digitalWrite(pin0_out,1);
//Wait 50 ms
delay(50);
//LED 1 ON
digitalWrite(pin1_out,1);
delay(50);
//LED 0 OFF
digitalWrite(pin0_out,0);
delay(50);
//LED 1 OFF
digitalWrite(pin1_out,0);
delay(50);
}
//Wait 100 ms
delay(100);
}
return 0;
}
As you can you see I used the famous wiringPi library that allows to use the GPIO ports of the raspi like an arduino.
Here an image to understand how pin works and how to identify it using wiringPi library
In this article we are working on the
- 1 (3.3v) - current on
- 3 (SDA0) - to read when we push the button
- 6 (0V) - ground
- 11 (GPIO 0) - to make LED1 blinks
- 12 (GPIO 1) - to make LED2 blinks pins.
Warning: pay attention to use the 1st pin, on the left. On the right there is a 2nd pin and it puts out 5v and can break your board.
Now connect your pushbutton : V3.3 -> 10kOhm -> 3 Pin -> PushButton -> 0v Now connect your leds: 11 pin -> 330 Ohm -> +LED1 -> -LED1 -> 0V and 12 pin -> 330 Ohm -> +LED2 -> -LED2 -> 0V
A simple image:
and now to compile and execute the source:
gcc source.c -o blink -lwiringPi ; sudo ./blink