Don't understand Analogue readings

I am using a LM393 light Sensor Module on a Pro Mini Module Atmega328. I am trying to work out how it all works using the following sketch:

/* YourDuino Electronic Brick Test: 
 Light Sensor Analog + Digital AB-890202
 terry@yourduino.com */

/*-----( Declare Constants )-----*/
#define ANALOG_SENSOR_PIN  A0 
#define DIGITAL_SENSOR_PIN 3
#define LEDPIN    13  // The onboard LED

/*-----( Declare Variables )-----*/
int  switch_state;  /* Holds the last digital value */
int  LightAnalogValue; /* Holds the last analog value */

void setup()   /*----( SETUP: RUNS ONCE )----*/
{
  pinMode(LEDPIN, OUTPUT);
  Serial.begin(9600);          // Enable the Serial data output
  Serial.println("YourDuino Light Sensor Test 1.10 ");  

}/*--(end setup )---*/


void loop()   /*----( LOOP: RUNS CONSTANTLY )----*/

// This module is ACTIVE LOW when a reflection is seen
{

  switch_state = digitalRead(DIGITAL_SENSOR_PIN);  
  if (switch_state == LOW)
  {
    digitalWrite(LEDPIN, HIGH);
    Serial.println("Digital Signal ON ");
  }  
  else
  {
    digitalWrite(LEDPIN, LOW);
  }

  LightAnalogValue = analogRead(ANALOG_SENSOR_PIN);  //Read the voltage from sensor
  Serial.print("Analog Value (0 to 1023)"); 
  Serial.println(LightAnalogValue,DEC);      // Send result to Serial Monitor
  delay(500);

}/* --(end main loop )-- */

/* ( THE END ) */

What I don’t understand is that the sensor only appears to change of I shine a bright light on it (If I shine a BRIGHT light on the sensor, the LED goes on). The output from the Analogue pin stays at 1023 until I shine a light at it at which point it drops straight down to the 40’s. I would expect it to change gradually as the light intensity changes. Do I need to connect it in serials with a resistor or something?

Do you have links to this LM393 light sensor? I always thought the LM393 was a comparator. Is the sensor using one of these to give a digital on/off reading on light level?

Did you connect A0 pin from sensor module to A0 pin on arduino ?

This may help: http://arduino-info.wikispaces.com/Brick-LightSensor-Analog-Digital

I am really new at this stuff and probably bitten off a larger chunk than I should have. But having done so I will try and see it through.

This was the original link that I used (which is same as the Wiki one mentioned above): http://arduino-info.wikispaces.com/Brick-LightSensor-Analog-Digital

I am using a similar LightSensor: http://www.aliexpress.com/item/LM393-light-Sensor-Module-3-3-5V-input-optical-sensor-module/1630968820.html

Just can't get it to work. AO doesn't appear to change unless a very bright light is shined at it.

There is something else I don't understand. The Analogue Inputs appear to always show a reading in the 300's even if nothing is connected to them (monitoring them via the serial monitor).

I have tried the above using other sensors and Pro Mini Module Atmega328 modules (got a few as I want to use them with a school class).. maybe I need to try with an Arduino Uno R3; but I can't see how this will make any difference,

Hi abasel

According to that web page, the light sensor generates a digital output (0V or +5V) depending on whether the light level is above a threshold. It looks like you can adjust the threshold by changing the potentiometer on the board. However, it says nothing about having an analogue output depending on the light level.

Regards

Ray

These are two different boards. One has analog out, the other not.
At least as far as I can tell.

There is something else I don't understand. The Analogue Inputs appear to always show a reading in the 300's even if nothing is connected to them (monitoring them via the serial monitor).

This is normal - unconected analog inputs are "hanging" and picking up noise etc. Try to connect this input to GND - then it should read zero.

Thanks for all the great feedback.

The board definitely has A0 and D0 labelled pins, but I have e-mailed the manufacturer to confirm. If it only has DO it will be a pity but I will still be able to use it as all I want to do is switch on a LED when it gets dark.

I will try the grounding option just to get my head around how it all works :-)

If you have a voltmeter, even a cheap one, then you can check this board just to be shure. Power up the sensor, connect one probe to gnd and second to this A0 pin and watch the voltage as you change the light. It it change smoot, then it is true analog output. If it jumps to certain level and stays there - then it's only digital.

Got it to work. The issue was I needed to set the potentiometer on the board.

Change the code a little to get the light to come on when it got dark.

/* YourDuino Electronic Brick Test: 
 Light Sensor Analog + Digital AB-890202
 terry@yourduino.com */

/*-----( Declare Constants )-----*/
#define ANALOG_SENSOR_PIN  A0 
#define DIGITAL_SENSOR_PIN 3
#define LEDPIN    13  // The onboard LED

/*-----( Declare Variables )-----*/
int  switch_state;  /* Holds the last digital value */
int  LightAnalogValue; /* Holds the last analog value */

void setup()   /*----( SETUP: RUNS ONCE )----*/
{
  pinMode(LEDPIN, OUTPUT);
  Serial.begin(9600);          // Enable the Serial data output
  Serial.println("YourDuino Light Sensor Test 1.10 ");  

}/*--(end setup )---*/


void loop()   /*----( LOOP: RUNS CONSTANTLY )----*/

// This module is ACTIVE LOW when a reflection is seen
{

  switch_state = digitalRead(DIGITAL_SENSOR_PIN);  
  if (switch_state == LOW)
  {
    digitalWrite(LEDPIN, LOW);
    Serial.println("Digital Signal OFF ");
  }  
  else
  {
    digitalWrite(LEDPIN, HIGH);
  }

  LightAnalogValue = analogRead(ANALOG_SENSOR_PIN);  //Read the voltage from sensor
  Serial.print("Analog Value (0 to 1023): "); 
  Serial.println(LightAnalogValue,DEC);      // Send result to Serial Monitor
  Serial.print("Digital Pin: "); 
  Serial.println(switch_state,DEC);      // Send result to Serial Monitor
  delay(500);

}/* --(end main loop )-- */

/* ( THE END ) */

Will try and use the Analogue input to deliver varied brightness to some LEDs and the digital to simply switch others on or off.

Thanks again

In the interests of completeness I show you the final code which turns the LED on when it gets dark, with the brightness getting darker as the night falls :-)

/*-----( Declare Constants )-----*/
#define ANALOG_SENSOR_PIN  A0 
#define DIGITAL_SENSOR_PIN 3
#define LEDPIN    11  // The onboard LED

/*-----( Declare Variables )-----*/
int  switch_state;  /* Holds the last digital value */
int lightLevel;
int ledLevel;

void setup()   /*----( SETUP: RUNS ONCE )----*/
{
  pinMode(LEDPIN, OUTPUT);
}/*--(end setup )---*/


void loop()   /*----( LOOP: RUNS CONSTANTLY )----*/
{
  switch_state = digitalRead(DIGITAL_SENSOR_PIN);  
  lightLevel = analogRead(ANALOG_SENSOR_PIN);
  ledLevel = lightLevel / 4;
  
  if (switch_state == HIGH)
  {
    analogWrite(LEDPIN,ledLevel);
  }  
  else
  {
    analogWrite(LEDPIN, LOW);
  } 
  
  delay(500);

}/* --(end main loop )-- */

/* ( THE END ) */

Got it to work. The issue was I needed to set the potentiometer on the board.

Glad you got it to work. I would have adjusted the pot first.