Log-Scale Light Sensor Issues Again

For other supported Arduino products from Adafruit: Shields, accessories, etc.

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Locked
daz1761
 
Posts: 130
Joined: Sun Mar 24, 2013 1:52 pm

Log-Scale Light Sensor Issues Again

Post by daz1761 »

I've just revised one of my data logging boxes and come across the same problem I had with my original build. The original build saw unsteady and random readings of light values e.g. 35, 36, 36, 35, 37, 35, 7, 0, 34, 14, 35, etc., which could of been the sign of a dry joint. My board was re-soldered and checked followed by the addition of new wires for other analog inputs that were left unconnected. This made the readings a bit more steady, but still not good or reliable.

The revised version saw an exchange in boards (Pro mini to Nano 3.1) all connected to another brand new proto-perma board. The problem still persists and I've no idea why. Just to test, I hooked up the log-scale sensor to my Uno, uploaded a simple analog read sketch and it was steady as can be with no anomalies.

All other analog channels are fine in both cases above. The voltage regulator is 5v and the capacitors are 0.1uf. External power is from a 9v battery, although it doesn't matter where the board gets its power from the same problem persists.

I've attached a diagram of how my power distribution is implemented and the code I'm using to read the values from the sensor.

Code: Select all

lightVal = map(constrain(lightVal, 0, 614), 0, 614, 0, 100);
Image

Please note I am using a microphone as an example as I could not find a log-scale sensor from within Fritzing. Although, it does have the same connections.

User avatar
adafruit_support_bill
 
Posts: 88090
Joined: Sat Feb 07, 2009 10:11 am

Re: Log-Scale Light Sensor Issues Again

Post by adafruit_support_bill »

Please post the complete code you are using and any output.

The sharp IR distance sensors are notorious noise generators, and that may be affecting the stability of your analog readings.

daz1761
 
Posts: 130
Joined: Sun Mar 24, 2013 1:52 pm

Re: Log-Scale Light Sensor Issues Again

Post by daz1761 »

Noise could be the culprit now you mentioned it. Is there a way this noise can be controlled?

Heres my code:

Code: Select all


#include <SD.h>
#include "Wire.h"
#include "DHT.h"
#include "RTClib.h"



#define DHTPIN 2       // Connect the DHT22 data pin
#define DHTTYPE DHT22   // DHT 22  (AM2302)
#define light_pin 0     //  Define light pin
#define ir_pin 1     //  Define ir pin
#define CS_pin 10  //  Chip Select SD card


int temp, humidity;  //  Variables for our temp and humidity
int lightVal;  //  Variable for light sensor
int irVal;  //  Variable for IR Sharp sensor
int id = 1;  //  ID for .csv file

RTC_DS1307 RTC;

DHT dht(DHTPIN, DHTTYPE);

void setup() {
 
  Serial.begin(9600);
  
  Wire.begin();  // Start I2c
  RTC.begin();  // Start RTC
  dht.begin();  // Start the DHT 
  
  pinMode(CS_pin, OUTPUT);
  
  //  Check if card is ready
  if(!SD.begin(CS_pin)) {
    Serial.println("Failed!");
    Serial.println();
    return;
  }
  Serial.println("Ready!");
  Serial.println();
}

void loop() {
  
  //lightSensor();
  
  lightVal = analogRead(light_pin);
  lightVal = map(constrain(lightVal, 0, 614), 0, 614, 0, 100);
  Serial.print("Light: ");
  Serial.println(lightVal);
  Serial.println();
  
  //irSensor();
  
  irVal = analogRead(ir_pin);
  irVal = map(constrain(irVal,82,419), 82, 419, 0, 100);
  Serial.print("Sharp IR: ");
  Serial.println(irVal);
  Serial.println();
  
  //tempAndHumidity();
  
  // Reading temperature or humidity takes about 250 milliseconds!
  // Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
  float humidity = dht.readHumidity();
  float temp = dht.readTemperature();

  // check if returns are valid, if they are NaN (not a number) then something went wrong!
  if (isnan(temp) || isnan(humidity)) {
    Serial.println("Failed to read from DHT");
    Serial.println();
  } 
  else {
    Serial.print("Humidity: "); 
    Serial.print(humidity);
    Serial.print(" %\t");
    Serial.print("Temperature: "); 
    Serial.print(temp);
    Serial.println(" *C");
    Serial.println();
  }
  
  //dateAndTime();
  
    DateTime now = RTC.now();
    
    Serial.print(now.year(), DEC);
    Serial.print('/');
    Serial.print(now.month(), DEC);
    Serial.print('/');
    Serial.print(now.day(), DEC);
    Serial.print(' ');
    Serial.print(now.hour(), DEC);
    Serial.print(':');
    Serial.print(now.minute(), DEC);
    Serial.print(':');
    Serial.println(now.second(), DEC);
    Serial.println();
  
  //sdWrite();
  
  File dataFile = SD.open("log.csv", FILE_WRITE);

  if(dataFile) {
    dataFile.print(id);
    dataFile.print(",");
    dataFile.print(temp);
    dataFile.print(",");
    dataFile.print(humidity);
    dataFile.print(",");
    dataFile.print(lightVal);
    dataFile.print(",");
    dataFile.print(irVal);
    dataFile.print(",");
    dataFile.print(now.hour(), DEC);
    dataFile.print(",");
    dataFile.print(now.minute(), DEC);
    dataFile.print(",");
    dataFile.print(now.second(), DEC);
    dataFile.print(",");
    dataFile.print(now.day(), DEC);
    dataFile.print(",");
    dataFile.print(now.month(), DEC);
    dataFile.print(",");
    dataFile.println(now.year(), DEC);
    dataFile.close();
 
    Serial.println("Logging!");
    Serial.println();
  }
  else {
    Serial.println("No SD Card!");
    Serial.println();
  }
  
  id++;  // Increment ID number
  
  delay(1000);
  
}

User avatar
adafruit_support_bill
 
Posts: 88090
Joined: Sat Feb 07, 2009 10:11 am

Re: Log-Scale Light Sensor Issues Again

Post by adafruit_support_bill »

You are doing single analog reads on two different channels. Due to the way the analog pins are multiplexed on the Arduino, it can take some time for the signal to settle when switching channels. A common remedy for this is to take multiple readings, throw away the first one, then take the average of the rest.

Throwing away the first one gives the signal time to settle. Averaging several samples helps to filter out high frequency noise.

As for noise from the IR sensor, the datasheet recommends: http://sharp-world.com/products/device/ ... 21yk_e.pdf
●Advice for the power supply
• In order to stabilize power supply line, we recommend to insert a by-pass capacitor of 10μF or more
between Vcc and GND near this product.
It is also a good idea to separate wiring as much as possible from your other sensors.

daz1761
 
Posts: 130
Joined: Sun Mar 24, 2013 1:52 pm

Re: Log-Scale Light Sensor Issues Again

Post by daz1761 »

Thanks for the tips :)

So to take multiple readings for example I would create a function (e.g. sampleLightSensor) that would say sample analog A0 ten times then return the average reading minus for the first value. What is the most acceptable sample time and samples? Also, if each channel takes some time to settle, would it be better to read multiple analog channels one after the other (e.g. light first, then delay, then the IR, etc.)?

Is a by-pass capacitor the same as a ceramic capacitor, and would they sit both sides of my power rails just as my 0.1uf capacitors are now?

User avatar
adafruit_support_bill
 
Posts: 88090
Joined: Sat Feb 07, 2009 10:11 am

Re: Log-Scale Light Sensor Issues Again

Post by adafruit_support_bill »

So to take multiple readings for example I would create a function (e.g. sampleLightSensor) that would say sample analog A0 ten times then return the average reading minus for the first value.
Yes. That is a good way to do it.
What is the most acceptable sample time and samples?
After throwing away the first sample, you can read the rest back-to-back without delay. The number of samples depends on how time critical your readings are. If you have plenty of time, take plenty of samples. I often due 5 or 10.
if each channel takes some time to settle, would it be better to read multiple analog channels one after the other (e.g. light first, then delay, then the IR, etc.)?
Since it is the changing of the channel (analog pin number) that requires the settling time, you want to read all the samples on a given channel together. If you have time to add further delay, add it between the first sample on a given channel and the subsequent samples on the same channel.
Is a by-pass capacitor the same as a ceramic capacitor, and would they sit both sides of my power rails just as my 0.1uf capacitors are now?
Yes. Ceramic is fine. Add another one right next to the IR sensor leads.

daz1761
 
Posts: 130
Joined: Sun Mar 24, 2013 1:52 pm

Re: Log-Scale Light Sensor Issues Again

Post by daz1761 »

Thanks again for the help.

So far I have added a 10uf electrolytic capacitor right next to the Sharp IR and written an function that will hopefully sample the light pin 10 times after the initial reading and wanted to check if its okay:

Code within the loop:

Code: Select all

//  Sample the light pin 10 times after the first initial reading 
  int lightVal = sampleLightPin(light_pin);
  
  lightVal = map(constrain(lightVal, 0, 614), 0, 614, 0, 100);
  Serial.print("Light: ");
  Serial.println(lightVal);
  Serial.println();


Function below the main loop:

Code: Select all

int sampleLightPin(int lightPin)
{
  long reading = 0;
  analogRead(light_pin);
  //  Ignore first analog read
  delay(1);
  
  for (int i = 0; i < sampleSize; i++)
  {
    reading += analogRead(light_pin);
  }
  
  return reading/sampleSize;
}

User avatar
adafruit_support_bill
 
Posts: 88090
Joined: Sat Feb 07, 2009 10:11 am

Re: Log-Scale Light Sensor Issues Again

Post by adafruit_support_bill »

Looks good! Let us know how it works for you.

daz1761
 
Posts: 130
Joined: Sun Mar 24, 2013 1:52 pm

Re: Log-Scale Light Sensor Issues Again

Post by daz1761 »

Its definitely better since implementing the new code, but still quite unsteady. For example, when pointing the sensor to my light bulb it will report values like this:

{55, 54, 51, 55, 55, 45, 51, 47, 55, 54, .., etc}

The good thing is that there is no anomalies like zeros, etc. If you think its the Sharp IR, I may look at removing it. In the meantime I may as well write another sampling function for the Sharp IR.

It may also mean implementing a type of moving averager filter within MatLab to hopefully eliminate the unsteadiness. I've also got my TSL2561 reporting at 16 bit Lux, but I am worried about the saturation (NaN) when the light is too bright, and wanted the log-scale sensor to back it up.

User avatar
adafruit_support_bill
 
Posts: 88090
Joined: Sat Feb 07, 2009 10:11 am

Re: Log-Scale Light Sensor Issues Again

Post by adafruit_support_bill »

For example, when pointing the sensor to my light bulb
I wouldn't go by any hand-held readings. Put the sensor on the table under the lamp and see how steady the readings are.
If you think its the Sharp IR, I may look at removing it.
That's easy enough to test. Just disconnect it for a bit and see if stability improves.

Locked
Please be positive and constructive with your questions and comments.

Return to “Other Arduino products from Adafruit”