In this lesson, we will show how to use a Button Switch module to turn on/off an LED through Arduino Graphic programming technology.
Both LED and Switch module will be connected to OSOYOO Magic I/O shield. Here, “I/O” refers to the INPUT and OUTPUT. OSOYOO Magic I/O Shield is an extension board which can help use to easily connect sensors, actuators and Robot car control ports to Arduino I/O pins.
We will plug LED module into D8 port of the shield and plug Switch module to D2 port on the shield. We will tell Arduino D8 will be an OUTPUT port which will send out signal to control LED. D2 is an INPUT port which gets input signal from Switch button.
Arduino IDE (version 1.6.4+)
The Button Switch module is signal generator device. When button pressed, SIG pin will generate HIGH voltage signal, when button is released, SIG pin will generate LOW voltage signal.
LED module will be turn on when SIG pin get HIGH voltage signal and will turn off when SIG pin gets LOW voltage signal.
First, please plug Osoyoo Magic I/O shield into UNO board. Then connect the LED and Button module to the D3 and D9 port of the Magic I/O shield with a 3-pin PNP cable as below:
LED Module – D3
Button Module – D9
After above operations are completed, connect the Arduino board to your computer using the USB cable. The green power LED (labelled PWR) should go on.
After the routine above done, you might think, if we want to turn on the LED in this way, the hand cannot leave button, it is not convenient.How to control the lights as normal , click on the light, then press out? We can improve the program, which can realize the result that will modify the program for the following code, and then upload to the Osoyoo Arduino board.
The circuit is same as above,you can get the sketch from this link or copy below code to your new Arduino IDE window and upload it to your Arduino board. Don’t forget to choose the corresponding board and port for you project!
#define LED 3 // Set D3 as the LED pin #define BUTTON 9 //Set D9 as the button pin //Let's say you have your push button on pin 12 int switchState = 0; // actual read value from pin12 int oldSwitchState = 0; // last read value from pin12 int lightsOn = 0; // is the switch on = 1 or off = 0 void setup() { pinMode(BUTTON, INPUT); // set the push button as input pinMode(LED, OUTPUT); // anything you want to control using a switch e.g. a Led } void loop() { switchState = digitalRead(BUTTON); // read the pushButton State if (switchState != oldSwitchState) // catch change { oldSwitchState = switchState; if (switchState == HIGH) { // toggle lightsOn = !lightsOn; } } if(lightsOn) { digitalWrite(LED, HIGH); // set the LED on } else { digitalWrite(LED, LOW); // set the LED off } }
Open the Arduino IDE and select corresponding board type and port type for your Arduino board.
Now, simply click the “Upload” button in the environment. Wait a few seconds – you should see the RX and TX leds on the board flashing. If the upload is successful, the message “Done uploading.” will appear in the status bar.
After the program is compiled and uploaded, you can achieve: click the button to turn on the LED, and then press this button, the LED will be turned off.
DownLoad Url osoyoo.com