Monday, July 27, 2020

Arduino Tutorial - Light Sensor

Arduino Tutorial - Light Sensor

Light Sensor

Introduction

  • For this project, we are going to use a light sensor to detect the light levels then turn on an LED when it is dark.
  • For the light sensor, we are going to use what is called a phototransistor.
    • They are a form of bipolar transistor which are sensitive to light sources.
  • For an overview of phototransistors, please see this article

Materials

  • Arduino
  • Breadboard
  • wires
  • (one) phototransistor
  • (one) LED
  • (one) 220 ohm resistor
  • (one) 10k ohm resistor

Circuit

Here is the circuit diagram for this project.

Things to note:

  • make sure the long leg(anode) of the phototransistor is connected to 5v of the arduino
  • the short leg(cathode) of the phototranistor is connected to the 10k ohm resistor and to A1 on the arduino.
  • the phototransistor creates a current proportional to the light level. pin A1 is reading the voltage level at the 10k resistor due to this current.

Here is the circuit i built for this project

Code

Here is code for this project. the entire code can be found here

things to note:

  • the analog in pins(A0 - A5) do not need to be set up with pinMode as an input pins.
  • analogRead() is a function which reads in an analog value.
    • it reads a 10 bit number between 0 and 1023
    • in our case, 0 would be no light and 1023 would be the brightest light which the phototranisotr is capable of resolving
    • for more on analogRead(), please see the article here

Final Conclusions,Remarks,and Results

  • if all went well, the LED should light up when it is dark. you may need to adjust the value in the if statement to get it to work correctly in your case.
  • analogRead() can be used to read in an analog signal, but you have to remember that it will be between 0 and 1023.
    • this means that you may need to adjust for the analog signal you are reading in.you can do this a few ways, one of them is the map() function.
      • I will be covering this function in a later tutorial.

Going further

Things to do to continue this project:

  • try adjusting the value in the if to only turn on with different levels of light.
  • you could also go the other way, have the LED turn off with certain levels of light
  • try to incorporate what we learned about analogWrite in a previous tutorial to control the brightness of the LED
    • try adjusting the brightness of the LED based on the value you get from the light sensor.

Thank you for reading through my tutorial. please check back again for the next part of my arduino tutorial.

No comments:

Post a Comment

Arduinot Tutorial - Serial Monitor

Arduino Tutorial - Serial Monitor Introduction ...