Photoresistors are among the most popular lighting level sensors for Arduino. They are easy to use, and yet, there are some unexpected surprises, especially when we try to use ready modules designed by somebody else.
In this Instructable I will show you how to connect a ready Photoresistor module to Arduino, how to compensate the signal for the way the module is designed, and how to control a digital signal ON and OFF based on the level of the lighting.
Please note that the annotation on some of the pictures in this Instructable will work correctly on some browsers only after you click on the image to expand it.
Please make sure you click on the image to see the annotation showing the correct spot on the image!
Step 1: Components
Step 2: Connect the Photoresistor Sensor to Arduino
- Connect Ground(Black wire), Power(Red wire), and Signal(Yellow wire) to the Photoresistor Module(Picture 1)
- Connect the other end of the Ground wire(Black wire) to the Ground pin of the Arduino board(Picture 2)
- Connect the other end of the Power wire(Red wire) to the 5V power pin of the Arduino board(Picture 2)
- Connect the other end of the Signal wire(Yellow wire) to the Analog pin 0 of the Arduino board(Picture 2)
- Picture 3 shows where are the Ground, 5V Power, and Analog 0 pins of the Arduino Nano
Step 3: Start Visuino, and Select the Arduino Board Type
To start programming the Arduino, you will need to have the Arduino IDE installed from here: http://www.arduino.cc/.
Please be aware that there are some critical bugs in Arduino IDE 1.6.6.
Make sure that you install 1.6.7 or 1.6.5, otherwise this Instructable will not work!
Step 4: In Visuino: Connect the Analog 0 Channel to the Serial Channel
In Visuino connect the "Out" pin of the Digital[ 14 ]/Analog[ 0 ] channel of the Arduino component (Picture 1) to the "In" input pin of the Serial[ 0 ] channel of the Arduino component (Picture 2)
Step 5: Generate, Compile, and Upload the Arduino Code
- In Visuino, Press F9 or click on the button shown on Picture 1 to generate the Arduino code, and open the Arduino IDE
- In the Arduino IDE, click on the Upload button, to compile and upload the code (Picture 2)
Step 6: And Play...
Picture 1 shows the running project.
Picture 2 shows the complete Visuinodiagram.
In Visuino, click on the Scope tab, select serial port, and click on the Connect button (Picture 3)
You should see the data from the sensor plotted on the scope(Picture 4)
If you cover the Photoresistor, you will notice that the value is increasing, and if you point it to light, it is decreasing. The reason is that the Photoresistor in this module has been connected to ground as you will see in the next step.
Step 7: Option 1: Correct the Signal by Switch the Power and Ground
Picture 1 hows how the photoresistor on this module is connected.
Since this is a passive module - there are not any other elements beside the photoresistor, and one other resistor, you can switch the power and ground (Picture 2) and the the connection will become as shown on Picture 3
Picture 4 shows the newly connected project.
If you connect to it with Visuinoas shown in the previous step, you will see that this time the value will increase if the light in the photo resistor increases.
Step 8: Option 2: Correct the Signal in Visuino
If you don't want to switch the Power and Ground, you can correct the signal in Visuinoby using a Subtract From Value component and subtract from 1.0 :
- Type "sub" in the Filter box of the Component Toolbox then select the "Subtract From Value" component (Picture 1), and drop it in the design area
- Disconnect the old connection between Analog[ 0 ] and and Serial[ 0 ] (Picture 2)
- Connect the "Out" pin of the Digital[ 14 ]/Analog[ 0 ] channel of the Arduino component to the "In" pin of the SubtractFromValue1 component (Picture 3)
- Connect the "Out" pin of the SubtractFromValue1 component (Picture 4) to the "In" input pin of the Serial[ 0 ] channel of the Arduino component(Picture 5)
- In the Object Inspector, set the value of the "Value" property of the SubtractFromValue1 component to 1 (Picture 6)
Step 9: And Play...
Step 10: In Visuino: Add a Compare Component to Control the LED on Digital Pin 13
- Type "com" in the Filter box of the Component Toolbox then select the "Compare Analog Value" component (Picture 1), and drop it in the design area
- In the Object Inspector, set the value of the "Value" property of the CompareAnalogValue1 component to a value between 0.4 to 0.7 (depending on your lighting) (Picture 2)
- In the Object Inspector, set the value of the "CompareType" property of the CompareAnalogValue1 component to ctSmaller (Picture 3)
- Connect the "Out" pin of the SubtractFromValue1 to the "In" pin of the CompareAnalogValue1 (Picture 4)
- Connect the "Out" pin of the CompareAnalogValue1 to the "Digital" input pin of the Digital[ 13 ] channel of the Arduino component(Picture 5)
Step 11: And Play...
Generate code, compile and upload as you did in Step 5.
If you run the project in Arduino, and if there is enough light, the LED on pin 13 will be OFF as shown on Picture 1.
If you cover the Photoresistor, the LED on pin 13 will turn ON as shown on Picture 2.
Congratulations! You have mastered using Photoresistor module with Arduino, and Visuino.