LED auto blink

Learn how to turn on and off the LED on the Boltduino automatically

This is the second project that you will make using the Boltduino.
It is just as simple as the last project, but you will learn how to use delays with your Arduino code using this project.

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.

Prerequisite

  • 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.
479
  • Go to File>New. A new Arduino Sketch will be created.
309
  • At the top of the code, copy and paste the following code snippet. View the image below for refrence.
#include <BoltDeviceCredentials.h>
499
  • Go to File>Save.
289
  • In the file saving screen enter the name "LED auto blink" and click on save. The name of the sketch will change to LED_auto_blink.
714 505

You have successfully created a Boltduino sketch for the LED auto blink 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);
  • Copy the following code snippet and paste it into the loop function, as shown in the image below.
digitalWrite(LED_BUILTIN,HIGH);
delay(1000);
digitalWrite(LED_BUILTIN,LOW);
delay(1000);
502
  • Go to File>Save.
289
  • Click on the verify (✔) button. The Arduino IDE will compile the code.
503
  • Once the code is verified, the Arduino IDE will show you a "Done Compiling" message.
499

You have completed writing the code to blink the LED on the Boltduino automatically.

Connect the hardware

  • Connect the Bolt unit to your Boltduino, and power up the Boltduino using one of the following 3 methods.
4640

Using a DC adapter.

4640

Using the Boltduino's micro USB port.

4640

Using the Bolt's mirco USB port.

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.
503
  • Once the code is uploaded the Arduino IDE will show you "Done uploading" message.
502
  • Look at the LED on your Boltduino. It will turn on and off continuously once every second.
    HURRAY!! You have now completed your second Boltduino project.

Code Explanation

  • This code had 5 lines of code. That is 3 lines more than the previous project.
  • If you read the first line written in the setup function, it is familiar.
  • The line 'pinMode(LED_BUILTIN,OUTPUT);' tells the Boltduino to set the 'LED_BUILTIN' pin as output.
  • The first line is in the loop is also familiar.
  • The line 'digitalWrite(LED_BUILTIN, HIGH);' tells the Boltduino to set the 'LED_BUILTIN' pin 'HIGH', effectively turning on the LED.
  • The next line is new.
  • The 'delay' function takes an integer as the only argument.
  • The argument tells the Boltduino how many milliseconds to wait before running the next command.
  • A millisecond is a thousand times smaller time frame then one second.
  • So 1000 milliseconds is the same as 1 second, and the Boltduino will wait at this function for 1 second, before running the next function.
  • The next line as you may have guessed, tells the Boltduino to set the 'LED_BUILTIN' pin 'LOW, effectively turning off the LED.
  • Then you have another delay of 1 second so that you can see the LED turning off.
738

But wait, why does the Boltduino switch the LED on again, after reaching the last step??

Well, the secret behind this mystery is the fact that the code was written in the loop function and not the setup function.

The Boltduino keeps on running the instructions in the loop function over an over again. So once it is done with the last line which is 'delay(1000);' it goes back to the first line and turns the LED back on.

Experiment

Now that you know how to turn on and off the LED in an automated manner, it's time for a challenge.
Figure out how you can have the Boltduino turn the LED on and off faster.

HINT: You can do this by changing the time the Boltduino waits between turning on and turning off the led.

The End

Were you able to make the LED blink automatically? Click here to talk to us if you faced any issues.

Click here to give us feedback regarding this documentation.


What’s Next

Now that you know how to control the LED on the Boltduino in an automated manner, why not try controlling external LEDs?
Head on over to the next project to find out how.