LXF 146 Arduino Tutorial

Code junkies hangout here

Moderators: ChrisThornett, LXF moderators

LXF 146 Arduino Tutorial

Postby madoba » Sat Aug 06, 2011 4:26 am

Anyone complete this Project? (Turning a light on or off depending on the current lighting level). I have the code compiled and uploaded on the arduino but I am unsure how to connect the resistor and LED to the arduino. Anyone willing to help out a arduino beginner?

For this project I have I think all the components, the arduino software, the arduino one board and a 10k -Ohm resistor
and a green LED with a build-in-resistor.

Could it be I don't have the right components. Is A light dependent resistor the same as a LED with a built in resistor?
And how the heck does the 10k - Ohm resistor work in all this? It's basically a small straight wire with some component stuff in the middle and where the heck does it go?

Anyway I'm just a beginner and still learning and researching it.

I went online and found an easier arduino tutorial and was able to successful compile and upload a (blink program) to the arduino and succesfully connected the LED to the board but the tutorial in LXF 146 is giving me some problems.

Any help would be appreciated.
madoba
 
Posts: 14
Joined: Wed Jul 06, 2011 1:50 pm
Location: San Angelo, Texas, USA

LXF 146 Arduino Tutorial

Postby Bruno » Sat Aug 06, 2011 4:18 pm

Hi madoba,

I've not completed the project, but I may be able to lend a hand.

From the list of components you give, you don't seem to have an LDR (Light Dependent Resistor). However, the green LED that you do list is still needed here, just elsewhere. For this tutorial, the LED should be connected to the board as it is for the blink programme, so you are on the right track here.

To answer your question, an LDR is not the same thing as an LED (Light Emitting Diode). An LDR is like the 10k ohm resistor but its resistance varies with the amount of light falling on it. They typically have a high resistance in the dark (a few megaohms) and a lower resistance in light (a few kiloohms), depending on the brightness of your light source. I've found the following links to show you what an LDR looks like: LDR picture 1 and LDR picture 2. These two pictures may not look that similar but the difference between them is mostly due to how the sensor is packaged. To connect the 10k (see [1]) resistor and the LDR to the board correctly, connect one leg of the LDR to the "5V" pin, the other leg of the LDR to the 10k resistor and the other, free leg of the 10k resistor to the "GND" pin. Now connect the junction made between the LDR and the 10k resistor to the "A1" pin.

To answer your other question, there are two things going on here with the 10k resistor and the LDR (see [2]). The first thing is that the 10k resistor will set the maximum current that can flow through the LDR (and itself). If the LDR were in very bright light, its resistance would be close to zero and therefore the current through the LDR would be nearly equal to 5 (volts) divided by the resistance of the ordinary, invariable resistor. Using a 10k resistor here gives a maximum current of 0.0005 amps (or 0.5 milliamps), by Ohm's Law (V = I * R). As there will also be a voltage drop across the LDR (more on this later), the voltage dropped multiplied by the current will give the amount of power the LDR has to dissipate (P = V * I). As this power is dissipated as heat, too much and the LDR will be damaged or even melted/burned. Hence, careful selection of the value of this invariable resistor will protect the LDR. The second thing (hinted at earlier) is that the two resistors will divide up the 5 volts that you have put across them in proportion to their resistances. Hence, this circuit fragment is called a voltage divider (or potential divider, see the link on how they work). As the resistance of the LDR changes with the amount of light that shines on it, the voltage at the junction between the LDR and the 10k resistor also changes, as a result of this voltage division. As this voltage is sensed by the board at pin "A1", you now have a light level sensor to which your Arduino can be programmed to respond.

I hope this helps, sorry if you know any of this already but as you say you are starting out, I thought some detail was appropriate. Enjoy your adventures with Arduino and let me know here how you get on.

All the best,
Bruno


[1] As resistance is always measured in ohms, electronic engineers usually omit this (the unit) in circuit diagrams and just give the number and a multiplier of the value of the resistor, so k = kilo, 1,000; M = mega, 1,000,000 etc., thus 10k is 10,000 ohms.

[2] Resistors are known as passive components and as such just do what their resistance value (and ability to dissipate heat) dictates. They don't "work" as such. That blob (some sort of plastic or resin) in the middle of the "wire" contains the resistance and the two legs (both good conductors) connect to either end of the resistance. It only looks like a blob on a single piece of wire.
User avatar
Bruno
LXF regular
 
Posts: 139
Joined: Tue Sep 18, 2007 6:07 pm
Location: Cambridgeshire, UK

Postby madoba » Mon Aug 08, 2011 12:39 am

Hey Bruno thinks for the info, I suspected that. So what I did was went up to radio shack and picked up a LDR, and a breadboard, wire and other components, and after spending a few hours researching arduino on the internet, it's all starting to make sense. I'm even starting to understand the pictures in the article, and yes even the resistor thingy.

Anyway one thing I have'nt figured out yet, And i've put the project away until next weekend, is why the value from the analogRead(LDR) code varies so much. I thought this thing was a light detector and if so why are the values jumping all around the place, is'nt the light consistent. I don't quite understand why it does this. I've added code to turn off the LED if the value is over 500, and on, if it's below 500 and the LED keeps turning off and on like a christmas tree.

// ** Main loop **************************************************
void loop() {
val = analogRead(LDR); //read the sensor value

Serial.print("light reading:");
Serial.println(val);
if (val > 500) {
digitalWrite(LED, LOW);
delay(2000); }
else {
digitalWrite(LED, HIGH);
delay(2000); }

}


P.S. Hey how's that media player working you built in the tuxradar tutorial?
madoba
 
Posts: 14
Joined: Wed Jul 06, 2011 1:50 pm
Location: San Angelo, Texas, USA

Postby Bruno » Sat Aug 13, 2011 7:35 pm

Hi madoba,

Glad to see things are coming together.

How much does the value from the analogRead(LDR) change? I just rigged up the circuit on my board and got a fairly consistent 700 +/- 5. If I move around or move some objects around, I can get variations of +/- 300 or so but not when I'm still.

Could the LDR be picking up varying light levels from changing images on your monitor or reflections of a moving object, such as a fan? Also, does the value oscillate between two fairly consistent values? If so, the LDR may be picking up light from the LED. Does it point towards it? When the LED is on, your sketch could be commanding it to switch off and vice versa.

I hope this helps. Thanks for asking about the Qt tutorial. If you post something on that topic's page, I'll let you know how I'm getting on.

Cheers,
Bruno
User avatar
Bruno
LXF regular
 
Posts: 139
Joined: Tue Sep 18, 2007 6:07 pm
Location: Cambridgeshire, UK

Variable resistor help

Postby Larry » Fri Aug 26, 2011 7:37 am

Hi modoba:

I see you're making some progress in your quest to get this thing going. The part you are looking for is a cadmium sulfide photo cell. These items are comparatively old technology. I recall seeing them in articles when vacuum tubes, (electron valves), were still very much in use. As you indicated, they are readily available at your Radio Shack. I recently picked up a package of them to repair some defective solar side walk lights. You get a package of five or so in different sizes. For your application, any of them should do. Regarding the bouncing readings you are measuring, these could be an artifact of florescent lighting in the area. These things pulsate at a rate of 60Hz in the U.S., and 50 Hz in the U.K. Depending on the sample rate of the program, you could be sensing light levels at different times of the pulse cycle. To see if I am correct, replace the photo cell with a 1 M Ohm variable resistor. If the the value changes, but remains consistent as you rotate the resistor's knob, then it is most likely pulsating lights in the room. As for how to connect the electronics to the micro controller board, there is a color figure in the upper right hand corner of page #15. There is also a more complete wiring diagram, including the Schmitt trigger, pictured at the top of page #14.

Hope this helps.
Larry
 
Posts: 18
Joined: Tue Aug 23, 2011 5:16 am

Postby madoba » Sat Nov 26, 2011 8:52 pm

Bruno you were right, (wow this was a long time in coming) the values were varying due to the monitor because I was running the arduino right next to it. OOPPS! Sadly :( this was the only arduino project I attempted. I'll have to try to take some other Arduino projects up again sometime. Thanks!
madoba
 
Posts: 14
Joined: Wed Jul 06, 2011 1:50 pm
Location: San Angelo, Texas, USA


Return to Programming

Who is online

Users browsing this forum: No registered users and 0 guests