Coding a Sensor – Photocell

In doing the next part of my assignment, Coding a Sensor, I needed to be sure I was using the correctly rated resistor of 10KΩ. I had two kinds, so I had to figure out which was which. After learning a little on Wikipedia: Electronic Color Code
Resistor Color Coding

I found a Resistor Calculator on HobbyHour.Com that was helpful. The one I had been using is what I need, 10KΩ. I’m having trouble distinguishing the others I have because I can’t really tell the difference between black and blue in the second column to well. If it’s black it’s 500KΩ, but the calculator warns me that is a Non-standard 5% (E24) value! If I use blue it doesn’t warn and says that it’s 560KΩ. That number seems high, but I don’t know enough to know if it’s high or not. Anyway, I have the resistor I need.

I read a little about the Light-Dependent Resistor (LDR) from a resource on bildr.blog:

sensor

Simple Light Reading With LDR + Arduino – bildr.blog

“The LDR changes its resistance with light so we can measure that change using one of the Arduino’s analog pins. But to do that we need a fixed resistor (not changing) that we can use for that comparison (We are using a 10K resistor). This is called a voltage divider and divides the 5v between the LDR and the resistor. Then we measure how much voltage is on the LDR using the analog read on your arduino, and we have our reading. The amount of that 5V that each part gets is proportional to its resistance.”

Okay. Got that.

“With the arduino analogRead, at 5V (its max) it would read 1023, and at 0v it read 0. So if the the LDR and the resistor have the same resistance, the 5V is split evenly (2.5V), to each part. (analogRead of 512)

But if the LDR is hit with a ton of light and is reading only 1K of resistance, the 10K resistor is going to soak up 10 times as much of that 5V. So the LDR would only get .45V (analogRead of 92).

And if it is in a dark room, the LDR may be 40K or resistance, so the LDR will soak up 4 times as much of that 5V as the 10K resistor. So the LDR would get 4V (analogRead of 818).”

OK – seems accessible so far…

I was given a tutorial to use to code the Arduino for the photocell, but I’ve grown so accustom to my breadboard that I get lost if the description entails alligator clips and wires. I like the breadboard as it lays things out well for me to contemplate what’s happening more carefully. So I found this tutorial from Adafruit: Using a Photocell.

12

Breadboard using 2 resistors, one LDR, and one LED. Ravine crossed for the LED.

photcell

int photocellPin = 0; // the cell and 10K pulldown are connected to a0
int photocellReading; // the analog reading from the sensor divider
int LEDpin = 11; // connect Red LED to pin 11 (PWM pin)
int LEDbrightness; //
void setup(void) {
// We’ll send debugging information via the Serial monitor
Serial.begin(9600);
}

void loop(void) {
photocellReading = analogRead(photocellPin);

Serial.print(“Analog reading = “);
Serial.println(photocellReading); // the raw analog reading

// LED gets brighter the darker it is at the sensor
// that means we have to -invert- the reading from 0-1023 back to 1023-0
photocellReading = 1023 – photocellReading;
//now we have to map 0-1023 to 0-255 since thats the range analogWrite uses
LEDbrightness = map(photocellReading, 0, 1023, 0, 255);
analogWrite(LEDpin, LEDbrightness);

delay(100);
}

• Which coding concepts were introduced by coding the sensor (photocell)?

 a) ≠coding, but learning about resistors/ohms was helpful.

 b) Using variables again in a new form:
Serial.print(“Analog reading = “);
Serial.println(photocellReading);
LEDbrightness = map(photocellReading, 0, 1023, 0, 255);
analogWrite(LEDpin, LEDbrightness);

• Describe the computational thinking that you are practicing when creating/coding with the sensor (photocell):

Now that we are bringing in analog readings into the mix I’m having to keep two different levels of thinking in mind at the same time. I’m struggling with that a little now.

Leave a Reply