Introduction: Arduino Nano: Using Passive Photo Resistor Sensor With Visuino

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

Picture of Components
  1. One Arduino compatible board (I use Arduino Nano, because I have one, but any other will be just fine)
  2. One Photoresistor Sensor module I got from this cheap 37 sensors set
  3. 3 Female-Female jumper wires

Step 2: Connect the Photoresistor Sensor to Arduino

Picture of Connect the Photoresistor Sensor to Arduino
  1. Connect Ground(Black wire), Power(Red wire), and Signal(Yellow wire) to the Photoresistor Module(Picture 1)
  2. Connect the other end of the Ground wire(Black wire) to the Ground pin of the Arduino board(Picture 2)
  3. Connect the other end of the Power wire(Red wire) to the 5V power pin of the Arduino board(Picture 2)
  4. Connect the other end of the Signal wire(Yellow wire) to the Analog pin 0 of the Arduino board(Picture 2)
  5. 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

Picture of 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!

  1. The Visuino: https://www.visuino.com also needs to be installed.
  2. Start Visuinoas shown in the first picture
  3. Click on the "Tools" button on the Arduino component (Picture 1) in Visuino
  4. When the dialog appears, select Arduino Nano as shown in Picture 2

Step 4: In Visuino: Connect the Analog 0 Channel to the Serial Channel

Picture of 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

Picture of Generate, Compile, and Upload the Arduino Code
  1. In Visuino, Press F9 or click on the button shown on Picture 1 to generate the Arduino code, and open the Arduino IDE
  2. In the Arduino IDE, click on the Upload button, to compile and upload the code (Picture 2)

Step 6: And Play...

Picture of 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 of 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

Picture of 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 :

  1. 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
  2. Disconnect the old connection between Analog[ 0 ] and and Serial[ 0 ] (Picture 2)
  3. 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)
  4. 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)
  5. In the Object Inspector, set the value of the "Value" property of the SubtractFromValue1 component to 1 (Picture 6)

Step 9: And Play...

Picture of And Play...

On the picture you can see the completed Visuino diagram.

Generate code, compile and upload as you did in Step 5.

if you connect to Arduino with Visuino as shown in Step 6, you will see that the value will increase if the light in the photo resistor increases.

Step 10: In Visuino: Add a Compare Component to Control the LED on Digital Pin 13

Picture of In Visuino: Add a Compare Component to Control the LED on Digital Pin 13
  1. 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
  2. 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)
  3. In the Object Inspector, set the value of the "CompareType" property of the CompareAnalogValue1 component to ctSmaller (Picture 3)
  4. Connect the "Out" pin of the SubtractFromValue1 to the "In" pin of the CompareAnalogValue1 (Picture 4)
  5. 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...

Picture of 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.

On Picture 3 you can see the complete Visuinodiagram.
Also attached are the Visuinoprojects, that I created for this Instructable. You can download and open them in Visuino: https://www.visuino.com

Comments