Tuesday, April 17, 2012

Alternating between a buzzer and an led using a push button and arduino

In this post we will control a led and buzzer using a push button and aroudino uno, turning them on alternately i.e. on one push of the button led is turned on buzzer is turned off and in the next push the led is turned off and buzzer is turned on.

Here are the circuit connections in the breadboard view and the schema view.

The led is connected to digital I/O pin 7 which will be set as digital output

The buzzer is connected to digital I/O pin 3 which will also be set as digital output

The digital I/O pin 2 acts as the input and is connected to the switch.

Each time the push button is pressed it sends a high on the pin 2 which on being read as high, pin 3(buzzer) is set to the value of the variable alt, which starts with a HIGH, and the pin 7(led) is set to negation of alt( !alt ) which will be LOW.

In the next iteration alt starts with LOW and the opposite happens, led gets a HIGH, the buzzer gets a low.

Thus the output switches between the led and buzzer on each push of the button.

Here is the program for the same.


Compile and load the above program into the aroudino board and on each press of the button the buzzer and led should turn on alternately.

No comments:

Post a Comment

Follow by Email