Button Input
Learn how to control an LED using a Button and Boltduino.
This is the fourth project that you will make using the Boltduino.
In this project, you will learn how to interface a button to the Boltduino, and use it to control an LED.
This project assumes that you have done all the steps in the "Setup the Boltduino" section and set up a global Bolt API key and Device Id for your Arduino IDE. If you have not done the setup, you can find out how to do this setup by clicking here.
Prerequisites
- Internet connection with 256kBps speed or better.
- A computer with 32 bit/64 bit Windows, Linux (Ubuntu preferred) or Mac os. Arm based computers are not supported.
- Arduino IDE 1.8.4 or later. Click here to get the latest Arduino IDE.
- Bolt Cloud account.
- Bolt unit with Firmware version 1.2.0 or higher. To find out how to update your Bolt unit click here.
- Boltduino with power supply.
- 1 x LEDs
- 1 x Micro Switch
- 1 x 330ohm resistors.
- 1 x 10 Kohm resistors.
- 1 small breadboard.
- 4 x Male to Male berge pin connectors as per requirement.
Create a Boltduino sketch
- Open the Arduino IDE.
- Go to File>New. A new Arduino Sketch will be created.
- At the top of the code, copy and paste the following code snippet. View the image below for refrence.
#include <BoltDeviceCredentials.h>
- Go to File>Save.
- In the file saving screen enter the name "Button Input" and click on save. The name of the sketch will change to Button_Input.
You have successfully created a Boltduino sketch for the Button Input project.
Write the code
- Copy the following code snippet and paste it above the setup function, as shown in the image below.
#define BUTTON_INPUT 2
#define LED_OUTPUT 3
- Copy the following code snippet and paste it into the setup function, as shown in the image below.
pinMode(LED_OUTPUT,OUTPUT);
pinMode(BUTTON_INPUT,INPUT);
- Copy the following code snippet and paste it into the loop function, as shown in the image below.
bool buttonState;
buttonState=digitalRead(BUTTON_INPUT);
digitalWrite(LED_OUTPUT,buttonState);
- Go to File>Save.
- Click on the verify (✔) button. The Arduino IDE will compile the code.
- Once the code is verified, the Arduino IDE will show you a "Done Compiling" message.
You have completed writing the code to blink the LED on the Boltduino automatically.
Connect the hardware
- Make the connections as per the following Fritzing diagram
- Connect the Bolt to the Boltduino and power the system up using one of the following methods.
Upload the code
- To upload the code click on the upload (->) button. The Arduino IDE will start uploading the code to the Boltduino via the Bolt Cloud OTA system.
- Once the code is uploaded the Arduino IDE will show you "Done uploading" message.
- Press the button, to make the LED glow. Leave the button to make the LED stop glowing.
HURRAY!! You can now control an LEDs using a button.
Code Explanation
- The instructions '#define BUTTON_INPUT 2' and '#define LED_OUTPUT 3' tell the Arduino IDE to replace all instances of 'BUTTON_INPUT' and 'LED_OUTPUT' with the pin numbers 2 and 3 respectively. We learned this in the previous project.
- The instruction 'pinMode(LED_OUTPUT, OUTPUT);' tells the Boltduino to set pin number 3 as an output pin.
- As you might have guessed the instruction 'pinMode(BUTTON_INPUT, INPUT);' tells the Boltduino to set pin number 2 as an input pin.
- The instruction 'bool buttonState' tells the Boltduino to create a boolean variable, which can store the states 'true' for 'HIGH' and 'false' for 'LOW'.
- The function 'digitalRead', takes a pin number as an input, reads the state of the pin and returns this state.
- So the instruction 'buttonState=digitalRead(BUTTON_INPUT);' tells the Boltduino to read the state of pin number 2, and store the state which is either 'HIGH' or 'LOW' in the buttonState variable.
- As we have seen in the earlier project, the instruction 'digitalWrite(LED_OUTPUT,buttonState);' tells the Boltduino to set the state of pin number 3 as per the value stored in the variable 'buttonState'.
- Since these 3 instructions are written in the loop function, the Boltduino keeps on repeating these instructions over and over again.
- The button in the above circuit connects or disconnects pin number 2 to 5v when it is pressed or released respectively.
- The 10kOhm resistor tris to pull the voltage on the pin number 2 to GND, but is only able to do that when the button is released.
- The Boltduino interprets a voltage of 5v as a 'HIGH' state and a voltage of 0v (GND) as a 'LOW' state.
- So when you press the button, the voltage on pin number 2 becomes 5v, the Boltduino reads this voltage, determines that the state of the pin number 2 is 'HIGH', and sets the state of pin number 3 to 'HIGH', turning the LED on.
- When you release the button, the voltage on pin number 2 becomes 0v (GND), the Boltduino reads this voltage, determines that the state of the pin number 2 is 'LOW', and sets the state of pin number 3 to 'LOW', turning the LED off.
Experiment
Amazing!!
So now that you know, how to program a Boltduino to turn an LED on when the button is pressed, and turn the LED off when the button is released, how about tinkering with the circuit a little bit so that it will do exactly the opposite?
HINT: To make it so that the Boltduino turns the LED off and when the button is pressed and turns the LED on when the button is released, switch connections of the microswitch, and the 10Kohm resistor.
The End
Were you able to use the button to control the LED? Click here to talk to us if you faced any issues.
Click here to give us feedback regarding this documentation.
Updated 4 days ago
Now that you know how to control an LED using a button and the Boltduino, head over to the next project to learn how to control the intensity of an LED.