Turn on LED
Learn how to turn on and turn off the LED on the Boltduino
This is the first project that you will make using the Boltduino. All you will do is write the code for turning on the LED on the Boltduino, upload it to the Boltduino, and then write the code for turning off the LED on the Boltduino and upload it to the Boltduino. The project is much more simple then it seems.
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.
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 "Turn On LED" and click on save. The name of the sketch will change to Turn_On_LED
You have successfully created a Boltduino sketch for the Turn On LED project.
Write the code
- Copy the following code snippet and paste it into the setup function, as shown in the image below.
pinMode(LED_BUILTIN,OUTPUT);
digitalWrite(LED_BUILTIN,HIGH);
- 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 turn on the LED on the Boltduino.
Connect the hardware
- Connect the Bolt unit to your Boltduino, and power up the Boltduino using one of the following 3 methods.
NOTE: If you have not already done so, set up the Bolt WiFi unit to connect to your Cloud account. Click here to find out how to set up your Bolt WiFi module.
- Wait for the Blue and Green LED of the Bolt WiFi module to turn on and become stable.
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.
- Look at the LED on your Boltduino. It will be turned on.
HURRAY!! You have now completed your first Boltduino project.
Code Explanation
- This code had only 2 lines.
- The first line is the 'pinMode' function.
- The 'pinMode' function is used to tell the Boltduino to set a pin as input or as output.
- This function which takes 2 arguments.
- The first argument tells the Boltduino which pin has to be set as output.
- The second argument for this function tells the Boltduino whether to set the pin as input or output.
- The second line is the 'digitalWrite' function.
- The 'digitalWrite' function tells the Boltduino to either set a specific pin, HIGH or LOW.
- The function takes 2 arguments.
- The first argument tells the Boltduino which pin to set 'HIGH' or 'LOW'.
- The second argument tells the Boltduino whether to set the intended pin as 'HIGH' or 'LOW'.
- When the pin is set to 'HIGH', the LED connected to the pin turns on.
- When the pin is set to 'LOW', the LED connected to the pin turns off.
- In our case we are using the 'pinMode' function to set the 'LED_BUILTIN' pin as an output pin, and the 'digitalWrite' function to set the 'LED_BUILTIN' pin 'HIGH'.
- The Boltduino knows the 'LED_BUILTIN' pin to be pin number 13, and the onboard LED is connected to this pin.
Experiment
Now that you know how to turn on an LED, you must be wondering how to turn off the LED.
Why don't you try it out?
HINT: You can turn off the LED, by using the same steps as above. Just replace, 'HIGH' with 'LOW' in your code.
The End
Were you able to turn on 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
Once you are able to turn the LED off, you can try switching the LED on and off in succession, by changing the LOW value to HIGH, and then the HIGH value to low.
But this can get tireing real quick, becuase you have to type the changes and then hit the upload button, and then wait for the code to be verified and uploaded.
What if we tell you, there is a better method to have the LED turn on and off, and that once you implement the method you won't have to lift a finger to have the LED keep switching it's state?
Find out how in the next project.