LED Intensity Control

Learn how to control the Intensity of an LED with the Boltduino.

In this project, you will learn how to control the intensity of light that an LED emits. You might think that is is a difficult project, but trust us, this project is easy.

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 LED
  • 1 x 330ohm resistors.

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 Intensity Control" and click on save. The name of the sketch will change to LED_Intensity_Control.
715 519

You have successfully created a Boltduino sketch for the LED Intensity Control project.

Write the code

  • Copy the following code snippet and paste it above the setup function, as shown in the image below.
#define LED_PIN  3
#define INTENSITY_CHANGE_RATE 4
#define INTENSITY_INCREMENT 10
long NextChangeTime;
int Intensity;
520
  • Copy the following code snippet and paste it into the setup function, as shown in the image below.
pinMode(LED_PIN,OUTPUT);
Intensity=0;
analogWrite(LED_PIN,Intensity);
NextChangeTime=millis()+(1000/INTENSITY_CHANGE_RATE);
520
  • Copy the following code snippet and paste it into the loop function, as shown in the image below.
if(NextChangeTime>millis()){
  return;
}
Intensity=Intensity+INTENSITY_INCREMENT;
if(Intensity>255){
  Intensity=0;
}
analogWrite(LED_PIN,Intensity);
NextChangeTime=millis()+(1000/INTENSITY_CHANGE_RATE);
518
  • Go to File>Save.
289
  • Click on the verify (✔) button. The Arduino IDE will compile the code.
520
  • Once the code is verified, the Arduino IDE will show you a "Done Compiling" message.
519

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
497
  • Connect the Bolt to the Boltduino and power the system up using one of the following methods.
4640

Using a DC adapter.

4640

Using the Boltduino's micro USB port.

4640

Using the Bolt's mirco USB port.

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.
519
  • Once the code is uploaded the Arduino IDE will show you "Done uploading" message.
518
  • Look at the LED. The intensity of light that it emits will continuously rise to maximum intensity, then the LED will quickly go off, and then repeat the process.
486

HURRAY!! You can now control the intensity of an LED using the Boltduino.

Code Explanation

Experiment

Now that you know how to change the intensity of the LED in an automated manner, how about tinkering with the intensity change rate, and the intensity increment, to get a few cool

HINT: You can find out which pins of the Boltduino are Digital Pins (Digital I/O), by clicking here.

Things to Remember.

The analogWrite function can only be used on the pins 3, 5, 6, 9, 10 and 11. If you look on the Boltduino, you will see a '~' symbol next to these pin numbers. This symbol is present to remind you that you can use an analogWrite function with these pins.

This project touched upon 2 power concepts in engineering. One is the PWM technique, and the other is the asynchronous programming.

In asynchronous programming, we tell the systems to remember what to do a particular instance of time, instead of just waiting for a certain amount of time before doing the next thing. This is why this code is different from the LED auto blink project, where we used delays, which is considered a synchronous method of programming.

The End

Were you able to control the intensity of the LED? 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 intensity of an LED head over to the next project to learn how to control the intensity of an LED, using a potentiometer.