Multi LED Control
Learn how to interface multiple LEDs with the Boltduino.
This is the third project that you will make using the Boltduino.
This project is not as simple as the earlier once. But don't worry, we have got you covered. In this project, you will learn how to define new functions and also, a clever way to tell the Boltduino which pins it should control.
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.
- 4 x LEDs
- 4 x 330ohm resistors.
- 1 small breadboard.
- 5 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 "Multi LED Control" and click on save. The name of the sketch will change to Multi_LED_Control.
You have successfully created a Boltduino sketch for the Multi LED 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_PIN1 2
#define LED_PIN2 3
#define LED_PIN3 4
#define LED_PIN4 5
void setLEDStates(bool pin1_state, bool pin2_state, bool pin3_state, bool pin4_state){
digitalWrite(LED_PIN1,pin1_state);
digitalWrite(LED_PIN2,pin2_state);
digitalWrite(LED_PIN3,pin3_state);
digitalWrite(LED_PIN4,pin4_state);
}
- Copy the following code snippet and paste it into the setup function, as shown in the image below.
pinMode(LED_PIN1,OUTPUT);
pinMode(LED_PIN2,OUTPUT);
pinMode(LED_PIN3,OUTPUT);
pinMode(LED_PIN4,OUTPUT);
- Copy the following code snippet and paste it into the loop function, as shown in the image below.
setLEDStates(HIGH,LOW,LOW,LOW);
delay(1000);
setLEDStates(LOW,HIGH,LOW,LOW);
delay(1000);
setLEDStates(LOW,LOW,HIGH,LOW);
delay(1000);
setLEDStates(LOW,LOW,LOW,HIGH);
delay(1000);
- 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 LEDs glowing one after the other.
HURRAY!! You can now control multiple LEDs using the Boltduino.
Code Explanation
This code is much larger than the code we had written in the earlier projects, but it has only a few new things to learn.
- The '#define LED_PIN1 2' instruction tells the Arduino IDE to replace all instances of 'LED_PIN1' in the code with the number 2. Here 2 is the pin number to which the first LED is connected to.
- Arduino IDE replaces these instances while compiling the code. This helps you define a pin number at the start of the code and then use it at multiple places throughout the code.
- We already know how the setup function, the pinMode function, and the digitalWrite function work from the 'Turn On LED' project. Click here to go through the code explanation of the 'Turn On LED' project.
- We already know how the loop function and the delay function work from 'LED auto blink' project.Click here to go through the code explanation of the 'Turn On LED' project.
- In this code we defined a new function altogether, which is the 'setLEDStates'. As you might have already guessed, we defined this function to take 4 boolean values, which tell the function whether to set a corresponding LED on (HIGH), or off (LOW).
- A boolean value is a special type of variable in the Arduino environment, which stores 1 of 2 possible states. The state can be one of either 'true' or 'false'.
- But wait, the digitalWrite function is supposed to take a HIGH or a LOW, how does a boolean value which stores a 'true' or a 'false' be given to the digitalWrite function?
- The reason behind this is that the Arduino IDE has special headers which use the define instruction to define, 'HIGH' as true, and 'LOW' as false.
- You can try out an experiment in to check this. In the 'LED auto blink' code, replace 'HIGH' with true, and 'LOW' with false, and upload the code to the Boltduino. The Boltduino will blink the LED as though nothing changed.
Experiment
Now that you know how to control multiple pins of the Boltduino, how about trying an experiment to control the LED using different pins?
On the Boltduino change the connections of pins 2,3,4, and 5 to some other digital pins change the code accordingly and upload the code to the Boltduino.
If you are planning to change the pin numbers in the '#define' instruction, then you have may have understood, why it is clever to define the pins this way. If you didn't define the pins this way, you would have had to go through the whole code and replace all places where you mentioned the pin number 2, with the new pin. But with this method, you only have to change it in 1 place and the Arduino IDE will take care of the rest.
HINT: You can find out which pins of the Boltduino are Digital Pins (Digital I/O), by clicking here.
The End
Were you able to control multiple LEDs? 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 multiple LEDs using the Boltduino, head over to the next project to find out how us a Button to control an LED.