Light Intensity Monitoring
Learn how to interface multiple LDR's with the Boltduino and Bolt.
This is the first project in which you will send data to the Bolt Cloud using the Boltduino and Bolt. In this project, you will learn how to monitor the intensity of light falling at 2 different locations near the Boltduino.
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.
- 2 x LDRs
- 2 x 10 Kohm resistors.
- 1 small breadboard.
- Male to Male berge pin connectors as per requirement.
Create an IoT enabled 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 <BoltIoT-Arduino-Helper.h>
- Go to File>Save.
- In the file saving screen enter the name "Light Intensity Monitoring" and click on save. The name of the sketch will change to Light_Intensity_Monitoring.
You have successfully created an IoT enabled Boltduino sketch for the Light Intensity Monitoring project.
Write the code
- Copy the following code snippet and paste it above the setup function, as shown in the image below.
#define ANALOG_INPUT1 A0
#define ANALOG_INPUT2 A1
String getIntensity(String *arguments){
String returnString="";
returnString=returnString+String(analogRead(ANALOG_INPUT1))+",";
returnString=returnString+String(analogRead(ANALOG_INPUT2));
return returnString;
}
- Copy the following code snippet and paste it into the setup function, as shown in the image below.
pinMode(ANALOG_INPUT1,INPUT);
pinMode(ANALOG_INPUT2,INPUT);
boltiot.setCommandString("RD\r",getIntensity);
boltiot.begin(Serial);
- Copy the following code snippet and paste it into the loop function, as shown in the image below.
boltiot.handleCommand();
- 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.
Once all the connections are done, the system will look similar to the image below.
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.
The hardware setup for the Light Intensity Monitoring project is now complete.
Cloud Setup
- Log into your Bolt Cloud account by clicking here
- Navigate to the products tab and click on the Build button. (Skip if you have had already created a product)
- If you have already created a product on the Bolt Cloud, you can create a new product by clicking on the 'New Product' button.
- In the popup that appears, select the input device and UART radio buttons, and enter the product name as 'Light_Monitor'.
- Click on the 'Done' button. The popup will disappear, and a new product will be added to your products list.
- Select the new product and click on the 'Configure this product' button.
- In the drop-down menu for CSV values, select 2.
- Add the CSV names 'ldr1' and 'ldr2' and click on save.
- Go to the code configurations tab.
- Add a name for your code, and select the type as js in the drop-down menu.
- Add the following code into the coding block and click save, and then close the product configurations.
var lineGraph1 = new boltGraph();
lineGraph1.setChartType('lineGraph');
lineGraph1.setAxisName('time_stamp','ldr1');
lineGraph1.plotChart('time_stamp','ldr1');
var lineGraph2 = new boltGraph();
lineGraph2.setChartType('lineGraph');
lineGraph2.setAxisName('time_stamp','ldr2');
lineGraph2.plotChart('time_stamp','ldr2');
- Click on the link button.
- Select your Bolt device and click on ok.
- A new device entry will be displayed. Select the deploy configuration button, to deploy the product configurations to the Bolt unit.
- Next click on the view device button to view the graph.
- Initially there will only be 1 single value displayed. Refresh the page after about 15 minutes 3 more entries will be added to the graph. A new entry will be added every 5 minutes.
HURRAY!! You can now monitor the intensity of light falling on 2 different LDR's using a single Bolt and a Boltduino.
Code Explanation
Arduino Code##
- The instructions '#define ANALOG_INPUT1 A0' and '#define ANALOG_INPUT2 A1' tell the Arduino IDE to replace all instances of 'ANALOG_INPUT1' and 'ANALOG_INPUT2' with the pin numbers 'A0' and 'A1' respectively. We learned this in the previous project.
- The function 'getIntensity' is a special function which we define to help the Boltduino respond to the Bolt, whenever it asks for data to be sent to the Bolt Cloud.
- This function reads the analog values from pin 'A0' and 'A1', formats the analog values into a string in the form of comma separated values (CSVs for short), and returns this string.
- The instructions 'pinMode(ANALOG_INPUT1, INPUT);' and 'pinMode(ANALOG_INPUT2, INPUT);' tells the Boltduino to set pin number 'A0' and 'A1' as input pins.
- The function 'boltiot.setCommandString("RD\r",getIntensity);' tells the Boltduino that, whenever the Bolt sends the command string 'RD\r' run the 'getIntensity' function, and respond to the Bolt with the string that the function returns.
- The function 'boltiot.begin(Serial);' tells the Boltduino to receive instruction and send data to the Bolt using the hardware serial port. The connections for hardware serial are done internally on the Boltduino.
- The 'boltiot.handleCommand();' continuously looks for instructions received from the Bolt, and accordingly runs the command functions set by the 'setCommandString' function.
Bolt Cloud Product Code""
- The instruction 'var lineGraph1 = new boltGraph()' creates a new graph view, adds it to your Bolt device view and stores an object which can control the attributes of the graph in the 'lineGraph1' variable.
- The function 'setChartType' sets the chart type of the graph view. Both of our graphs are of the type 'lineGraph'.
- The function 'setAxisName' sets the axis names to be displayed in the graph view. Here we have set the x axis name as 'time_stamp' and the y axis name as 'ldr1' and
- The function 'plotChart' actually plots the data stored in the Bolt Cloud on the graph.
- These functions are repeated for 'ldr2' variable, because of which 2 graphs are visible on the device view.
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