Analog Input
Learn how to control the Intensity of an LED using a potentiometer and Boltduino.
In this project, you will learn how to interface a potentiometer to the Boltduino, and use it to control the intensity 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 330ohm resistors.
- 1 x 10 Kohm potentiometer.
- 1 small breadboard.
- 1 screw driver.
- 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 "Analog Input" and click on save. The name of the sketch will change to Analog_Input.
You have successfully created a Boltduino sketch for the Analog Input project.
Write the code
- Copy the following code snippet and paste it above the setup function, as shown in the image below.
#define ANALOG_INPUT A0
#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(ANALOG_INPUT,INPUT);
- Copy the following code snippet and paste it into the loop function, as shown in the image below.
int analogData,intensityData;
analogData=analogRead(ANALOG_INPUT);
intensityData=map(analogData,0,1024,0,255);
analogWrite(LED_OUTPUT,intensityData);
- 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 the intensity of an LED using a potentiometer.
Code Explanation
- The instructions '#define ANALOG_INPUT A0' and '#define LED_OUTPUT 3' tell the Arduino IDE to replace all instances of 'ANALOG_INPUT' and 'LED_OUTPUT' with the pin numbers 'A0' 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(ANALOG_INPUT, INPUT);' tells the Boltduino to set pin 'A0' as an input pin.
- The instruction 'int analogData,intensityData;' tells the Boltduino to create 2 integer variable, which can store the analog reading from the analog input pin 'A0' and the intensity that we will set on pin number 3.
- The function 'analogRead', takes a pin number as an input and converts the voltage on the corresponding pin to an integer number. A voltage of 0v or lower yields an integer value of 0, a voltage of 5v or more yields an integer value of 1024. Any voltage in between is accordingly scaled.
- For example, a voltage of 2.5 v yields an integer value of 512.
- So the instruction 'analogData=digitalRead(ANALOG_INPUT);' tells the Boltduino to read the voltage of pin 'A0', and store the corresponding integer value in the analogData variable.
- Now we know that 'analogRead' function gives us values in the range of 0 to 1024, whereas the function 'analogWrite' takes an intensity value in the range of 0 to 255. To convert a range of 0 to 1024, to a range of 0 to 255, we use the map function.
- The map function takes 5 inputs.
- The value to be mapped to a new range. For us, this is the analogData.
- The lower limit of the input value. For us, this is 0.
- The upper limit of the input value. For us, this is 1024.
- The lower limit of the output value. For us, this is 0.
- The upper limit of the output value. For us, this is 255.
- The instruction 'intensityData=map(analogData,0,1024,0,255);' maps, the value in analogData from a range of 0 to 1024, to a range of 0 to 255. For example for values such as 0, 256, 512, 1024 in the variable analogData, the instruction would set a value of 0, 63,127, 255 respectively in the intentsityData variable.
- As we have seen in the earlier project, the instruction 'analogWrite(LED_OUTPUT,intensityData);' tells the Boltduino to set the state of pin number 3 as per the value stored in the variable intensityData.
- Since these 4 instructions are written in the loop function, the Boltduino keeps on repeating these instructions over and over again.
- The potentiometer in the above circuit form a voltage divider between the 5v and GND pins, with the output connected to the A0 pin.
- If you set the potentiometer all to way to the GND pin side, the voltage on the A0 pin would be 0, which would set the intensity on the LED to 0, and the LED would go off.
- If you set the potentiometer all the way to the 5v pin side, the voltage on the A0 pin would be 5v, which would set the intensity on the LED to 255, and the LED would glow it's brightest.
- If you set the potentiometer somewhere in between, then the intensity of the LED would be proportional to how far from either side you have set the potentiometer.
Experiment
Amazing!!
So now that you know, how to program a Boltduino to set the intensity of an LED based on the position of the potentiometer, how about tinkering with the code a little bit so that it will do exactly the opposite?
HINT: To achieve this effect, you would have to change the values that have been fed to the map function. Try switching the 4th and 5th argument of the map function.
The End
Were you able to control the LED intensity using the potentiometer? Click here to talk to us if you faced any issues.
Click here to give us feedback regarding this documentation.
Updated 4 days ago
We are working towards getting more projects for you to do on the Boltduino. Click the link below to add yourself to the mailing list, and we will send you an email regarding new projects.