In a previous article, I introduced the basics of using LED circuits with Arduino.
Now, we’ll explore more possibilities by adding other electronic components.
We already learned how to use electrical signals to light up a LED.
This was an “output” from Arduino to the LED.
We controlled the LED by changing the pin number, the strength, and the timing to make it light up.
This time, we will control the strength and the timing based on the input of a sensor.
What exactly is a sensor?
You’ve probably heard this word before, but do you know exactly what a sensor does?
Particularly in the field of electronics, you can often hear the term “____ sensor”.
A sensor is an electronic component that is used as an input device.
To put it simply, a sensor is used in place of the five senses (touch, sight, smell, taste, and sound).
Instead of the 5 senses that human have, Arduino and computers are using sensors to determine their surroundings conditions.
Humans have five senses (or maybe six in some cases…), but regarding electronic parts, there is a variety of types of sensors available.
Let’s use a light sensor.
Light Sensor
A light sensor (also known as a “CdS cell”) has a large resistance value when placed in a dark location and a low resistance value when places in a light location. When a project involves a light sensor, it’s natural to think about a project that will create an action when it gets bright in the morning or dark at night.
Let’s do some hands-on tests!
First, let’s test the sensor to make sure that a change of brightness will lead in a change of the resistance value. To do that, let’s set up a circuit to control the brightness of the LED.
1 2 3 4 5 6 7 8 9 10 11 12 |
//setup – this is called when Arduino starts up void setup() { //set Arduino output to pin 13 pinMode(13, OUTPUT); } // after setting up the loop, the loop will continue until Arduino being unplugged void loop() { digitalWrite(13, HIGH); //set output to pin 13 to HIGH =5V delay(1000); //wait for 1000 milliseconds (1 second) digitalWrite(13, LOW); //set output to pin 13 to LOW = 0V delay(1000); //wait for 1000 milliseconds (1 second) } |
The light sensor is like a resistor. It has no plus-minus directionality. So, you can just connect it in. When it’s bright, the resistance value goes down. So, I’ve added a resistor to prevent the current from flowing into the LED. With this setup, let’s now use Arduino to run the program we wrote at the beginning to make the LED blink. When the LED starts blinking, put your hand over the light sensor and take it away. You should see the brightness of the LED change.
This is the basic property of a light sensor. In that case, it should be easy to make a useful tool that makes an LED light up when it gets dark. Let’s give that a try!
Let’s set aside Arduino for a moment, and just deal with the construction of the electronic circuits. In this case, by using additional components we can find a way to devise a circuit. Electricity basically flows across the path of least resistance. When the below circuit is placed in a well-lit area, the resistance value will be low, and electricity will flow towards the resistor. On the contrary, when placed in the dark, the resistance value will be high, and the electricity will flow toward the LED, causing it to light up.
However, with this circuit, even if the light sensor is in a dark place, the LED won’t get very bright. When used like this, Arduino is just a box for carrying electric current and therefore not much more powerful than a battery. Let’s use Arduino to pick up the value from the light sensor and do something with it.
Let’s use Arduino’s analog input to measure the value of the sensor. Arduino’s analog input is shown here where it says “ANALOG IN.”
To allow the analog input to get data, construct the circuit as shown below.
We’ve now created an input circuit to get a value from the light sensor.
Next, let’s create an Arduino program to test it out.
1 2 3 4 5 6 7 8 9 10 |
int val=0; //variable to hold input from sensor void setup() { Serial.begin(9800); //setting to send data to the monitor } void loop() { //get data from pin 0 of ANALOG IN val=analogRead(0); Serial.println(val/4); //send the input value to the monitor delay(100); } |
We have created a new function.
We’d like to confirm the value from the sensor on our PC display, so in the setup function we wrote “Serial.begin(9800)”. By setting this, we can use Serial.println() within the loop to display the sensor value on the screen. This allows us to confirm input values. It’s a common method for debugging in Arduino.
Then, we get an input value from the analog pin. In this case, there’s no need to set the pin in the setup function.
The function to read input is:
analogRead(pin number)- function to get analog input
We will place the value obtained from analogRead into the “val” variable.
The “variable” keyword will play a very important role as we learn more about programming.
A variable is used by a program to temporarily store data (such as values or characters).
Variable: use when a program temporarily store data
Also, we will use the Serial.println() function to display the sensor value stored in “val” to the screen.
The Arduino analog pins accept input values between 0 and 1023. For this reason, to do something with the values that the program receives, we must write a program that knows what to do with these values.
To check the actual operation on the display, you can go to “Tools” and select “Serial Monitor” to check the input value from the light sensor.
Can you see the values constantly changing?
Now that we’ve confirmed the light sensor values, let’s use them to affect the LED.
We will take the values input from the light sensor and send them to the analog output to control the LED.
1 2 3 4 5 6 7 8 9 10 11 |
int val=0; //this variable will keep the sensor value void setup() { Serial.begin(9800); //setting for the monitor output } void loop() { //get data from the 0 pin of ANALOG IN val=analogRead(0); Serial.println(val/4); //display input value in monitor analogWrite(3,val/4); //send the input value / 4 to analog output delay(100); } |
Compared to the previous program, there is an extra analogWrite() function.
analogWrite([pin #],[output value]); send to analog output
There is no need to set the analog output in the setup function.
Unlike the analog input, which accepts values between 0 and 1023, the analog output values are between 0 and 255.
For this reason, we divide the input value by 4 before sending it to the output.
*If the brightness doesn’t change much even when you cover the sensor, it’s possible that your sensor has a narrow output value range. In this case, you should try changing to a resistor with a higher resistance value.
We have now completed both the setup for the input and the output. Until now, we have connected the Arduino to our USB port, so it has been getting 5V from the USB connection. But once the program has been written, you may want to use the Arduino without your PC. In that case, you can connect the Arduino to a 6-20V (recommended values from 7-12V) power adapter. If you place the Arduino in a case, then you will have a stand-alone light that gets brighter when in a dark environment.
If you would like to improve the functionality by changing the brightness gradually depending on the level of darkness, you can do so using “if” statements. It’s possible to have the LED turn off completely when it is bright out.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
int val=0; //used for storing the value input from the sensor int ledVal = 0; //variable that control the LED output void setup() { Serial.begin(9800); //for setting output to the monitor } void loop() { //accept data from the 0 pin of ANALOG IN val=analogRead(0); ledVal = val/4; // assign that value to the LED output variable Serial.println(ledVal); //display the value on the monitor //set the four levels of brightness if(ledVal < 50){ //the light should be off when the values are under 50 analogWrite(3,0); //output is 0 } else if(ledVal < 100){ // for values between 50 and 100 analogWrite(3,100); //output value is 100 } else if(ledVal < 150){ //for values between 100 and 150 analogWrite(3,150); //output value is 150 } else if(ledVal < 200){ //for values between 150 and 200 analogWrite(3,200); //output value is 200 } else{ //any input above 200 will get the maximum output analogWrite(3,255); //output value is 255 } delay(100); } |
Let’s think about this example. Using the same technique, we could replace the LED with a piezoelectric buzzer, and have it buzz when it gets dark (or bright). By using a servo motor operating at a fixed angle of rotation, we could turn on a switch when it gets dark, and so on. Arduino can also be connected to internet via a LAN cable. So, the brightness value could be sent over the internet and displayed elsewhere, or it could be used for creating a totally different work by using different input and output. One of the funny things about Arduino is to remember all the things we’ve done, and to think about all the things that we could create.
Next, we’ll use a different parts to create even more stuff!