I have had success with the photoresistor TinkerKit project. There are two versions. One is included in the 16-file download, the other is on the web page, linked below. I got the website version to work immediately. The download package version didn’t work. By reading the comments and tinkering with the variables I got it to work. One of the differences between the two versions is that the web version blinks an LED when the current is low. The download blinks when high. To make both versions blink when ambient light is dim, I tinkered some more, and got both to blink when ambient light is dim. When I got the download version to do what I wanted, I deleted all the comments. The code is beautiful in its simplicity. Compare the two sketches side-by-side. See PDF file, uploaded with my post. My idea is to build a beam-break counter. When the photoresistor light beam is broken, it sends a high signal to a counter. That’s still beyond my ability, but any comments are appreciated.
Why stip out comments? They add nothing to the overhead. Someday you might want to revisit this and the comments will be gone. Just curious as to the motivation.
+1 to NOT removing the comments!!
If they had stripped the comments out of the download package version, you would have had nothing to use for
reading the comments and tinkering with the variables
Thanks. Can you suggest a way to send photoresistor response to the serial monitor? Not lumens, but Ohms, perhaps? I am looking at Tutorial 7, in which a temperature sensor sends temperature data to the serial monitor. Before I move on to the next tutorial I would like to get as much out of Tutorial 6 as possible.
brianuno:
Thanks. Can you suggest a way to send photoresistor response to the serial monitor? Not lumens, but Ohms, perhaps? I am looking at Tutorial 7, in which a temperature sensor sends temperature data to the serial monitor. Before I move on to the next tutorial I would like to get as much out of Tutorial 6 as possible.
This modified version of the Tutorial #6 code will print the raw value of the analogRead to the serial console each time it loops. The value ranges from 0 to 1023, corresponding to a reading of 0V to 5V at the sensor pin. You can also use the map() function to convert that to a more usable number first. https://www.arduino.cc/en/Reference/Map
/*
SparkFun Tinker Kit
Example sketch 06
PHOTORESISTOR
Read a photoresistor (light sensor) to detect "darkness" and turn on an LED when it is "dark" and turn back off again when it is "bright."
This sketch was written by SparkFun Electronics,
with lots of help from the Arduino community.
This code is completely free for any use.
Visit http://learn.sparkfun.com/products/2 for SIK information.
Visit http://www.arduino.cc to learn more about Arduino.
*/
// As usual, we'll create constants to name the pins we're using.
// This will make it easier to follow the code below.
const int sensorPin = 0;
const int ledPin = 9;
// We'll also set up some global variables for the light level a calibration value and
//and a raw light value
int lightCal;
int lightVal;
void setup()
{
// We'll set up the LED pin to be an output.
pinMode(ledPin, OUTPUT);
lightCal = analogRead(sensorPin);
//we will take a single reading from the light sensor and store it in the lightCal
//variable. This will give us a prelinary value to compare against in the loop
Serial.begin(9600); //set up serial port to work at 9600bps
}
void loop()
{
//Take a reading using analogRead() on sensor pin and store it in lightVal
lightVal = analogRead(sensorPin);
Serial.println(lightVal);
// write value of analogRead to serial port. value will range from 0 to 1023,
// corresponding to 0V to 5V
//if lightVal is less than our initial reading (lightCal) minus 50 it is dark and
//turn pin 9 HIGH. The (-50) part of the statement sets the sensitivity. The smaller
//the number the more sensitive the circuit will be to variances in light.
if(lightVal < lightCal - 50)
{
digitalWrite(9,HIGH);
}
//else, it is bright, turn pin 9 LOW
else
{
digitalWrite(9,LOW);
}
}
Cool. I am getting serial monitor readings between ~120, when the photoresistor is in the dark, and ~920 when it is exposed to indoor lighting. Your tutorial writing is exceptionally clear and concise, if I may say so. I noticed these new functions in your sketch:
int lightCal;
int lightVal;
lightCal = analogRead(sensorPin);
Serial.begin(9600);
lightVal = analogRead(sensorPin);
Serial.println(lightVal);
if(lightVal < lightCal - 50)
digitalWrite(9,HIGH);
digitalWrite(9,LOW);
Darrellg, your sketch works with the same wiring scheme as the previous project. The LED lights when the photoresistor is shaded, serial monitor prints low numbers; LED gets dim when exposed to ambient light, serial monitor prints high numbers. That’s important to me, because I am imagining a project with different sensors, and either a LED or buzzer, along with printed data, to verify that the sensor is working, and that it’s not my imagination.
I am also looking at this page, a glossary of serial function (and a few examples) https://www.arduino.cc/en/Reference/Serial
The possibilities are mind boggling!
The sketch I posted was copied from the link you posted in your original post above. I only added two statements. In setup(), I added ``` Serial.begin(9600); //set up serial port to work at 9600bps ``` and in loop(), I addedbrianuno:
Darrellg, your sketch works with the same wiring scheme as the previous project.
Serial.println(lightVal);
// write value of analogRead to serial port. value will range from 0 to 1023,
// corresponding to 0V to 5V
It’s often useful to add these to your sketches, especially as a debugging tool. It allows you to see what your variables are set to at various places in the sketch. You can also print text to the console that can tell you, for example, that your sketch is about to perform a certain step. If you put these throughout a problematic sketch, you can see where a sketch might be hanging up. Note that the Serial.begin only needs to appear once, in setup(), then Serial.println can be inserted as many times as you like. Serial.println adds a carriage return and newline, while its companion Serial.print will not. So, you could modify the sketch to
Serial.print("Current sensor value: ");
Serial.println(lightVal);
// write value of analogRead to serial port. value will range from 0 to 1023,
// corresponding to 0V to 5V
and it would print something like
Current sensor value: 120
I am starting to get the idea. When ready to try writing an original sketch, I will begin with the IDE new sketch outline, then write the first thing I want the sketch to do, verify that, and so on, one line at a time. I can hardly wait to advance beyond cut-and-paste existing sketches. This simple project is great for beginners. I picked up a few useful tips by searching forum posts. If at sometime in the future another beginner searches photoresistor and serial monitor, this post may be useful. Copied below is the sketch after cleaning-up. Don’t think I don’t appreciate lines and lines of comments! They are like directions. After you arrive, and know how to get there, you can do without the directions. They have been saved, believe me. Uploaded with this reply is a screen shot of the wiring schematic, and a screen print of the serial monitor printing data.
// photoresistor to serial monitor and LED
const int sensorPin = 0;
const int ledPin = 9;
int lightCal;
int lightVal;
void setup()
{
pinMode(ledPin, OUTPUT);
lightCal = analogRead(sensorPin);
Serial.begin(9600);
}
void loop()
{
lightVal = analogRead(sensorPin);
Serial.println(lightVal);
if(lightVal < lightCal - 50)
{
digitalWrite(9,HIGH);
}
else
{
digitalWrite(9,LOW);
}
}