Theoretically, LED Can Act as Light Emitter & Light Detector. Here is How To Use LED as Sensor For Arduino, Circuit & Code Explaining Sensors in Robotics. This plain LED is never used in real life as sensor as basically they are designed to emit light, the light sense works better in one direction, sensitivity is lesser. This is analog sensor. First, we are sharing the code & circuit diagram. This is not what exactly what we want to talk about.
LED as Sensor For Arduino : Basic of Using Sensors in Robotics
This article, LED as Sensor For Arduino is just a basic example of using sensors to control a desired action. It is better to read about sensor in technology.
In this guide LED as Sensor For Arduino, light will go away if the “sensor LED” is exposed to dark, the “lighting LED” will glow when that dark condition is present for a defined period.
---
We are not exactly playing with toys. We have talked about wheeled chassis in robotics and basic way to control a DC motor. Now, if you combine this knowledge or rather projects – you can make a basic moving thing which will move when light is off.
Yes, that basic imaginary logically right robot which can oddly move at dark not at light will become better if you stay and read our guides. Our guides, be it for digital photography or WordPress or server works are written in a way that you should read from that topic’s initial articles. We merge towards an action which is practical. That light sensor is basic behind proximity sensors, obstacle detection etc. Other websites do not have this hierarchal way of guides. Yes, if you read Darren Rowse’s blogs, you’ll feel the same pattern in different topics. We do not want that you waste money.
LED as Sensor For Arduino : Code & Circuit
Light will go away if the “sensor LED” is exposed to dark, the “lighting LED” will glow when that dark condition is present for a defined period.
Sensor LED will be attached to Analog (input)’s A3 pin and GND. LED which will glow will be attached on Pin 13 and GND. Here is circuit :
And here is the code :
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 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 | int led = 13; // the pin where you will put the LED int sensorpin = A3; // the analog pin where you put your sensorLED int resetteller = 0; // the rest are counters and variables to calculate with int sens = 0; int teller = 0; int basis = 1024; int test = 1024; int test2 = 1024; int test3 = 1024; // this are the values to play with to get better (or worse) results int marge = 5; // the space between a positive and negative reading int vertraging = 1; // the speed of the readings; a lower number is a higher speed int samples = 70; // the amount of samples to compare to make one reading int resetsamples = 30; // how many cycles to run the light on before you don't trust the value anymore void setup() { pinMode(led, OUTPUT); Serial.begin(9600); // no real part of the program, just for debugging for(teller =0; teller < samples; teller++) {// remember the lowest value out of many readings sens = analogRead(sensorpin); if (sens < basis){basis = sens;} delay(vertraging); // the sensor needs a delay here to catch its breath } } void loop() { for(teller =0; teller < samples; teller++) {// remember the lowest value out of many readings sens = analogRead(sensorpin); delay(vertraging); // the sensor needs a delay here to catch its breath if (sens < test){ test3 = sens; // remember the 3 lowest readings test2 = test3; test = test2;} } if (test < basis-marge && test2 < basis-marge && test3 < basis-marge){//all 3 low readings mus be < the basis reading digitalWrite(led, HIGH); resetteller++; // count how long the LED stays on } else{ digitalWrite(led, LOW); basis = test; // if the lowest test reading is higher than the basis, basis will be reset resetteller = 0; } if (resetteller > resetsamples){basis = test;}//if LED stays on to long, we don't trust it and reset basis Serial.print(basis);Serial.print(" ");Serial.print(test);Serial.print(" ");Serial.println(sens);//just for debugging test = 1024; } |
The code was shared in Instructables and we see no reason to change it. Analog of Arduino is not exactly great, code may work once, there is chance short circuit etc. It is basically not hugely great example for Arduino but great example for a basic sensor. Do not touch the board’s bottom with hand, it can short circuit the analog pins. At least in our long duration test it works. If does not work after once worked, then upload the Blink program & watch the blinking pattern of RX and TX while the Blink sketch is getting uploaded. Unplug USB and try other stuffs later. It may happen due to an effect. That is also a kind of sensor system. Arduino is not exactly a good standard to learn embedded electronics.