Pages: [1] 2   Go Down
Author Topic: Light Meter Programming  (Read 4710 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Newbie
*
Karma: 0
Posts: 12
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hello,

I'm hoping I can obtain some much needed help with the programming of a circuit. I have never programmed before so this is all new to me. Well I'm designing an ambient light sensor with a digital display showing the amount of lux detected. So pretty much, just a basic light meter with a digital display for the lux. Looking at other light meters on the market I believe I want the lux range to be from 0 to anywhere to 20k, 40k, 50k, or even 100k lux. The max lux range really doesn't matter as long as it's sufficient enough. With the help of a customer service member I have already ordered the main parts I believe I'll need for this circuit. Those parts being a photocell, a LCD screen (HD44780 chipset), and a micro-controller(Arduino Uno). I already have a breadboard, resistors, etc. If someone would be so kind to help me out by telling me how the easiest way to start constructing and programming this circuit I would greatly appreciate it. Diagrams or similar projects with my components would be great too. Thanks again.
Here are the web pages to the parts I have ordered so far:

(Photocell)
sparkfun.com/commerce/product_info.php?products_id=9088

(LCD Screen)
sparkfun.com/commerce/product_info.php?products_id=709

(Micro-controller)
sparkfun.com/commerce/product_info.php?products_id=9950
Logged

Global Moderator
Netherlands
Offline Offline
Shannon Member
*****
Karma: 168
Posts: 12430
In theory there is no difference between theory and practice, however in practice there are many...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Where "map to 0..5 Volts" is used as comment you need to convert the number to LUX. There may be a formula or you need to create a lookuptable. And you have to print to your display iso to the serial port.
Succes,
Rob

Code:
// Cnt is the number of reads that will be averaged
// pin is the analog pinnumber the LDR is connected to.
float ReadLDR(unsigned int cnt, int pin)
{
  if (cnt > 1000) cnt = 1000;
  else if (cnt == 0) cnt = 1;
  
  // multiple samples to stabilize
  float v;
  for (int i=0; i<cnt; i++) v += analogRead(pin);
  v = (v * 5.0) / 1023 / cnt;     // map to 0..5 Volts
  return v;
}

void setup()
{
  Serial.begin(9600);
  Serial.println("Start...");
}

void loop()
{
  float val = ReadLDR(200, 0);   // read 200 times from Analog 0.
  Serial.print(val, 2);                 // print it with 2 decimals
  Serial.println(" Volt.");
  delay(100);                            // and wait 0.1 second.
}
« Last Edit: November 06, 2010, 06:22:40 pm by robtillaart » Logged

Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

0
Offline Offline
Newbie
*
Karma: 0
Posts: 12
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Rob thank you so much for replying.

Again this is my first time programming so I'm unfamiliar with a lot of the terms. :o Would you mind explaining more of the basics about how this is all going to work and the 1st steps I have to do get the project started?

Thank you for the help.  smiley
Logged

Global Moderator
Netherlands
Offline Offline
Shannon Member
*****
Karma: 168
Posts: 12430
In theory there is no difference between theory and practice, however in practice there are many...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Connect the LDR to a resistor as show below. As the resistance of the LDR will vary with the amount of light the voltage at point X will vary. To gain better understanding I suggest to try different values for the resistor R,  to get a feeling how this sketch works. E.g. try the following 5 values for R : 1K 10K 100K 1M 10M. You will see that depending on the value of R the range of voltages (no light, max light) will change.


                   X
5V >-- [ LDR ] ---+--- [ R ] ---<  GND
                   |
                   |
           Arduino pin A0


Now to the software: The Arduino globally works as follows, when started it first will call the Setup() routine. In my example code it initializes the speed of the serial port to match the default value of the PC. Then a string is printed to see it works.

After calling Setup the arduino will call the Loop() routine over and over again. In my example a value from the LDR is read by calling ReadLDR(with some parameters), then this value which is a floating point var is printed to the serial port with 2 decimal places, and because it represents a voltage the code prints that too . Notice the difference between print en println. The latter adds a newline. Then the loop waits for 200 milliseconds and starts over again.

Then there is float ReadLDR(unsigned int cnt, int pin).
The signature states this function needs two variables, a counter and a pinnumber (abbreviated by lazy programmer) The pinnumber refers to the Analog0 pin where the hardware is connected to. The count is added to stabilize the reading by averaging multiple readings.

Code:
if (cnt > 1000) cnt = 1000;
  else if (cnt == 0) cnt = 1;
takes care that count will allways stay between 1 and 1000 as we must have at least 1 reading and not to much.

Code:
 float v;
  for (int i=0; i<cnt; i++) v += analogRead(pin);
we must declare a float for the value to return, here named v. Then we enter a for loop in which the analogRead(pin) reads a binary representation of the voltage at pin . This is a 0 for 0 volt and 1023 for 5.0 volt, 2Volt will return ~410 etc.
The construction v+= analogRead(pin) is equal to v = v + analogRead(pin). So in short the for loop adds up multiple readings.

Code:
v = (v * 5.0) / 1023 / cnt;
does several things. The / cnt takes care that the reading is averaged, the *5.0 / 1023 maps the value 0..1023 to a floating point value of 0..5.0 volt. This value is returned.

So this is about how the posted code works.

Rob

« Last Edit: November 07, 2010, 03:46:53 am by robtillaart » Logged

Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

Global Moderator
Netherlands
Offline Offline
Shannon Member
*****
Karma: 168
Posts: 12430
In theory there is no difference between theory and practice, however in practice there are many...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1271601484 might also worth reading
Logged

Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

0
Offline Offline
Newbie
*
Karma: 0
Posts: 12
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Rob once again THANK YOU SO MUCH for the great feedback. I appreciate it a lot.  ;D

I have a few questions. Well I have 3 main parts to this circuit; the LCD screen, the Arduino Uno, and the LDR. On your sketch I do not see the LCD screen. Where does this come into play?

Now for your code that you posted, what originally did that do or control?

You talk about converting the number to LUX. Could you please tell me more about what you mean by that. Also when I do that will it really be that easy just to put in the code and it should work in displaying the LUX on the screen after the circuit is built.

Thanks again. I look forward to your response.  
Logged

Global Moderator
Netherlands
Offline Offline
Shannon Member
*****
Karma: 168
Posts: 12430
In theory there is no difference between theory and practice, however in practice there are many...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
On your sketch I do not see the LCD screen. Where does this come into play?
I use mostly a serial LCD from parallax #27979 [4x20] as it is very easy to use and it uses only pin 1 (TX) Only when uploading a new sketch you witness the Arduino version of the matrix smiley

Quote
Now for your code that you posted, what originally did that do or control?
It is a testapp to see how many readings I had to do, to get a stabilized reading from the LDR.

Quote
You talk about converting the number to LUX. Could you please tell me more about what you mean by that
IN my code I convert the readings to Volts. You don't want volts, you wanted lux
Quote
Well I'm designing an ambient light sensor with a digital display showing the amount of lux detected.
So you need another formula:
When you cover the LDR with black velvet == "absolute dark" , you will have found the zero point. A simple google said http://www.emant.com/316002.page for the rest.

Rob
Logged

Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

0
Offline Offline
Newbie
*
Karma: 0
Posts: 12
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thanks Rob.

Now I read about how I will have to calibrate to show the proper lux detected. Is there a way I don't have to do this? Like maybe a light to frequency converter found here;

http://www.sparkfun.com/commerce/product_info.php?products_id=9768

or

http://www.sparkfun.com/commerce/product_info.php?products_id=8940


Thanks always!
Logged

Global Moderator
Netherlands
Offline Offline
Shannon Member
*****
Karma: 168
Posts: 12430
In theory there is no difference between theory and practice, however in practice there are many...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Very interesting sensor, I would try the first one as this has less lines smiley

Rob
Logged

Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

0
Offline Offline
Newbie
*
Karma: 0
Posts: 12
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I have done a lot more research about programming and feel more comfortable with the terms now. I have my arduino board and my computer up and now communicating. Anyways, I was wondering if someone could send me or create a similar code for my design (light meter displaying the amount of lux on a lcd screen) that I could edit to my needs. I haven't been able to find a good code to start with yet, so I'm hoping someone can help me out with that. I appreciate your help so much. THANKS!!!  
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 12
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hello,

I was hoping you could help me out. I have a question about the coding. I've attached a video and code for a light meter below. I was wondering if you could tell me for that code that someone has posted if that's all the code you need for it to work. I was talking to someone at school today about it and he wasn't sure but he was telling me about how that's not an entire code. (Even though the person who posted it said it was.) He talked about the beginning of the code and how it starts with "Serial.begin (9600) " He was telling me there is a code for that alone and I would need it along with the code that got posted. Something about a library code. I was confused with what he was saying. To me it looks like an entire code but then again I don't really know nothing about coding. Can you clear anything up for me? Thanks!!!


http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1202530375

« Last Edit: November 22, 2010, 06:52:21 pm by heberjm8 » Logged

Global Moderator
Netherlands
Offline Offline
Shannon Member
*****
Karma: 168
Posts: 12430
In theory there is no difference between theory and practice, however in practice there are many...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

The code you refer http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1202530375#2 to is a good starting point. Learning to program is do it, try, fail , retry, fail, retry again, fail, think, think deeper ..... aha!! ,  try, succes, lookback, understanding smiley

If you don't understand how coding the Arduino works there is an excellent PDF to get started see http://www.earthshineelectronics.com/files/ASKManualRev4.pdf
might takes sometime to download as it is over 100 pages.
Logged

Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

0
Offline Offline
Newbie
*
Karma: 0
Posts: 12
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

So is that the entire code or do I have to look up some code for serial.begin in a library somewhere? Thanks!
Logged

Global Moderator
Netherlands
Offline Offline
Shannon Member
*****
Karma: 168
Posts: 12430
In theory there is no difference between theory and practice, however in practice there are many...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

No it is not the entire code, if you don't understand "Serial.begin(9600)" please study the reference section http://arduino.cc/en/Reference/Serial . You can google "Arduino Serial reference".

As said in my previous mail , the code is a starting point that looks a bit like the code you have to develop for your need. So you could  changing it a little bit to understand in detail how it works.

I understand that you are not familiar with arduino so please download the PDF mentioned earlier to learn about how to work with an  Arduino.

This forum helps people with questions about Arduino, hardware and software. However we never do projects for people as most of us have their own projects to take care of.
« Last Edit: November 23, 2010, 10:38:39 am by robtillaart » Logged

Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

0
Offline Offline
Newbie
*
Karma: 0
Posts: 12
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Okay so I've made some great progress on my light meter since I last posted. I have successfully completed the “Hello World” project from the Arduino website (http://www.arduino.cc/en/Tutorial/LiquidCrystal). I've got the Arduino board communicating with the computer and now the LCD screen is correctly displaying the information. I have attached a picture of my project so you can see it. I feel like I'm almost there which feels great!
My last 2 parts I need to complete is hooking up the photocell that I have (http://www.sparkfun.com/products/9088) to the Arduino board and obtaining the right code so the LCD will display the amount of lux. Well I was able to find a few codes online and try them out but they did not work. Here is one of them I tried:

http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1202530375



The two links go together. The video is of that particular code working. I know this code does not display the amount of lux but it does display 4 different light conditions (pretty dark, dark, bright, pretty bright). I just wanted to see my photocell in action displaying the light conditions on my lcd screen. When I hooked up the photocell and uploaded the code my lcd screen just went blank for some reason. This is how I hooked up the photocell which I found on a different webpage:

               5V >--[ Photocell ]---+---[ 10k Ohm ]---< GND
                                              |
                                   Arduino pin A0

I tried resetting the board and uploading the code many times. I also uploaded the “Hello World” code in between testing and that would work perfectly every time. So the possible 2 issues I think I'm having are that I do not know the type of photocell the person used or how exactly he hooked up the photocell. Even if I didn't know his exact photocell he used I'm thinking I should still be able to see a light condition being displayed on my lcd even if it's a wrong light condition.
I also found this code (http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1255287054) which looks to be the best and the exact code I would need for my application but it also just makes the LCD screen go blank for some reason. Can anyone please help me out? I would so greatly appreciate it. THANK YOU SO MUCH!  [/img]
Logged

Pages: [1] 2   Go Up
Jump to: