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.
- 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 "LED Intensity Control" and click on save. The name of the sketch will change to LED_Intensity_Control.
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;
- 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);
- 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);
- 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.
- 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.
HURRAY!! You can now control the intensity of an LED using the Boltduino.
Code Explanation
- The '#define LED_PIN 3' statement, we tell the Boltduino that the LED is connected to pin number 3.
- The '#defien INTENSITY_CHANGE_RATE 4' statement helps the Boltduino to calculate when it should change the intensity of the LED. This statement helps us define how many times in a second, the Boltduino should change the intensity of the LED.
- The '#defien INTENSITY_INCREMENT 10' statement helps the Boltduino to calculate how much it should change the intensity of the LED.
- The statements, 'long NextChangeTime' and 'int Intensity', tell the Boltduino that it will have to permanently remember the values of these variables. When we change the value held by these variables, the Boltduino remembers the changed values. A variable of the type 'long' is best used to store the time at which something has to be done, while a variable of the type 'int' is useful in storing integers.
- The 'pinMode(LED_PIN, OUTPUT);' tells the Boltduino to set pin number 3 as an output pin.
- We then tell the Boltduino to remember the intensity value which is set as the intensity of the LED.
- The function 'analogWrite(LED_PIN, Intensity);' changes the voltage on pin number 3, as per the value stored in the variable Intensity. For a value of 0 in the variable, the voltage on pin 3 is set to 0v, and for a value of 255, the voltage is set to 5v.
- For all other values, the voltage is changed relatively. The Boltduino does this by using a technique called the PWM. It does not actually change the voltage to the exact value, but quickly switches the voltage between 0 and 5 volts, such that the average voltage is somewhere in between these 2 voltages. You can read more about the PWM technique by clicking here.
- We then tell the Boltduino to calculate when it should change the intensity again. This is done by:
- Finding the current time using the 'millis()' function. The 'millis()' function tells the Boltduino the exact number of milliseconds that have passed since the Boltduino powered up.
- Dividing 1000 (There are 1000 milliseconds in 1 second), by the intensity change rate.
- Adding the result to the current time that the 'millis()' function gave us.
- The Boltduino then runs the loop function over and over and does the following.
- The instruction 'if(NextChangeTime<millis())' tells the Boltduino if the current time has not gone past the next intensity change time.
- If it is not yet time to change the intensity, (for example, 'millis()' gives 400, but the 'NextChangeTime' is 500) then the Boltduino runs the 'return;' instruction and exits the loop function.
- If it is time to change the intensity, (for example, 'millis()' gives 501 or higher, but the 'NextChangeTime' is 500), then the Boltduino runs the 'Intensity=Intensity+INTENSITY_INCREMENT;' instruction.
- This instruction tells the Boltduino to calculate the next intensity which should be set on the LED.
- Remember that the 'analogWrite()' function can only set an intensity in the range of 0 to 255. So with the next few instructions, we tell the Boltduino that, if the next calculated intensity is above 255, set it back to 0.
- We then tell the Boltduino to set the calculated intensity to the LED using the 'analogWrite' function.
- The next instruction then tells the Boltdunino to again calculate when it has to change the Intensity of the light next.
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.
Updated 4 days ago
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.