If you are following our previous blogs (#1, #2, #3, #4), you would realize that so far we worked on connecting an LED (an output device!) to Genuino controller board. A microcontroller (in this case, the Genuino processor) can write (or show) data to an output device. But What if you want to write (or send) data to the microcontroller? Here we will take up a simple input device connection.
An input device connects the physical world with the microcontroller system by providing input to it. It means, in simple terms, microcontroller can read the data from an input device. A simple switch is a common example of a binary input device – based on switch position, the microcontroller reads the value – on / off or high / low or 1 / 0.
Based on how a switch is connected, it can give HIGH (or logic 1), or LOW (or logic 0) signal that can be read by a microcontroller.
The grove kit also includes a bunch of input devices. We will look at ‘push button’ module first. We will connect a button module and LED module to the Genuino board using Grove Base Shield and then turn the LED on when button is pressed.
So let’s take out the Genuino board, Grove Base Shield, a connector, LED module with an LED and button module. Connect then as show below:
- You will have to put the Grove base shield on top of Arduino
- Connect the LED module to D3 of base shield using connector cable
- Insert the LED into the LED module – take a note of polarity! The shorter lead or one near the flat spot is the ‘cathode’ or ‘negative’ terminal of the LED.
- Connect the button module to D2 of the base shield using connector
and, like always, we follow the 4 basic steps:
- Connect the Genuino board to computer using USB cable
- Open the Arduino IDE (the software)
- Open the sketch – BUTTON example code using the menu: File > Examples > 02.Digital > Button
- Ensure the BOARD and PORT settings are correct: Tools > Board & Tools > Port
In the sketch, you’ll have to modify the ‘LED Pin’ from 13 to 3, as we connected the LED module to D3.
Compile and upload the sketch, you should see LED glow whenever the button is pressed.
- Try making the LED blink (1 flash per second) as long as button is pressed. LED should turn off when button is released.
- You may also like to try another variant – The LED flashes as long as button is pressed. Releasing of button make LED retain its state i.e., if the button is released when LED was on, it should remain ON (or vice versa).