Go Down

Topic: LED Light Sensor Circuit Inverted (Read 5251 times) previous topic - next topic

IanDG

Hi All

I'm struggling to understand where I'm going wrong with a very simple circuit.

I have light sensitive resistor that is being used to switch on an LED when the light level drops below a fixed value.  I'm monitoring the analogue value returned by the sensor and the value being used to set the LED via the serial window.  The values are as expected so I think the error is in the very simple LED circuit.

Code: [Select]

int sensePin = 0;
int sensorValue = 0;
int ledPin = 9;


void setup(){
  analogReference(DEFAULT);

  pinMode(ledPin, OUTPUT);

  Serial.begin(9600);
}


void loop(){

  int val = analogRead(sensePin);

  Serial.println(val);

  boolean ledVal = LOW;

  if (val < 750)
  {
    ledVal = HIGH;
  }
  else
  {
    ledVal = LOW;
  }

  digitalWrite(ledPin,ledVal);

  Serial.println(ledVal);

  delay(500);

}


The attached jpg contains a circuit diagram that hopefully is a close enough representation for my error to be identified.

The LED is being switched on and off as the light levels change but the wrong way around; it is on when the ledVal is LOW and off when the ledVal is HIGH.

Any suggestions as to where I am going wrong would be appreciated.

kind regards

Ian

JimboZA

#1
Dec 30, 2012, 08:20 pm Last Edit: Dec 30, 2012, 09:24 pm by JimboZA Reason: 1
Ian,

According to your diagram, you have the LED between the Arduino digital pin 9 and 5v?

So then when the pin is high, which is 5v, there will be no PD across the LED and it will be off although you expect it to go on. Then when the pin is low, the LED is across 5v to ground and be on, when you think it will be off.

Correct wiring is to put the LED and its series resistor between pin 9 and ground. Then when the pin is high, there will be pd across the led and it's on; when the pin is low, the LED is at 0v both sides and will be off.

Edit.... as shown here although it shows the LED on pin 13

HTH?
Johannesburg hams call me: ZS6JMB on Highveld rep 145.7875 (-600 & 88.5 tone)
Dr Perry Cox: "Help me to help you, help me to help you...."
Your answer may already be here: https://forum.arduino.cc/index.php?topic=384198.0

IanDG

Hi Jimbo

Thanks for the reply. 

I actually had the LED and it's resistor between the 5v and GND with Arduino Digital Pin 9 also in the same circuit which I take to be wrong.

I've modified my circuit as you suggested and illustrated by the link in the edit, but unfortunately the LED is still behaving in the same manner as before, with it coming on when I'm setting Pin 9 LOW and off when I set Pin 9 HIGH.

Another other suggestions as to where I'm going wrong?

Kind regards

Ian

JimboZA

Not sure why it might be inverted Ian... but wth, why not cheat and invert the logic so it comes out "right" even though that's "wrong"  :smiley-eek:
Johannesburg hams call me: ZS6JMB on Highveld rep 145.7875 (-600 & 88.5 tone)
Dr Perry Cox: "Help me to help you, help me to help you...."
Your answer may already be here: https://forum.arduino.cc/index.php?topic=384198.0

PeterH



I've modified my circuit as you suggested and illustrated by the link in the edit, but unfortunately the LED is still behaving in the same manner as before


Post your new circuit? The previous version was wrong because the Arduino would be trying to ground the LED without any current limiting resistor when the output was LOW. This could easily damage the LED and/or Arduino output pin.

IanDG

Hi Peter

Here is my new circuit:-



I should point out that I am as uncertain of the conventions used in circuit illustrations as I am of circuits themselves; hopefully it is sufficient to convey what I have on the board.

Kind regards

Ian

johncc


Hi All

I'm struggling to understand where I'm going wrong with a very simple circuit.

I have light sensitive resistor that is being used to switch on an LED when the light level drops below a fixed value.  I'm monitoring the analogue value returned by the sensor and the value being used to set the LED via the serial window.  The values are as expected so I think the error is in the very simple LED circuit.

Code: [Select]

  int val = analogRead(sensePin);
  boolean ledVal = LOW;
  if (val < 750)
  {
    ledVal = HIGH;
  }
  else
  {
    ledVal = LOW;
  }
  digitalWrite(ledPin,ledVal);

  Serial.println(ledVal);

  delay(500);

}


The LED is being switched on and off as the light levels change but the wrong way around; it is on when the ledVal is LOW and off when the ledVal is HIGH.


From what I can tell, val will be great when there is much light, and val will be small when there is little light.  Therefore, the code above will cause the led to light when the ambient light is low.  Is that what it's doing?  If you want the opposite, code
Code: [Select]

if (val < 750)
  {
    ledVal = LOW;
  }
  else
  {
    ledVal = HIGH;
  }


Hope this helps!
John

IanDG

Hi John

Thanks for the reply.

You are quite right in saying the ledVal should be HIGH when the light levels are low and vice versa.  The Serial.println(ledVal) is confirming that this is the case; it is writing out 0 when the values are over 750 and 1 when under 750.

However the LED is being illuminated when the light levels are high even though the value of ledVal is LOW.  This is the opposite of how I intended the circuit to work.  I know I could just fudge the values in the code but the point was to learn what I was doing and I am clearly getting something wrong.

Kind regards

Ian

johncc


If that is all the case then it may be that your circuit is not actually as you last posted?
If you remove the resistor lead from D9 and apply +5 to that lead, does the LED light?
If not, if you apply GND to that lead, does it light?
You know that the LED must be put in the right way and not backwards, right?

John

johncc

Also it might simplify your debugging if you use a sketch like this

void setup(){ pinMode(9,OUTPUT); digitalWrite(9, HIGH); }
void loop(){}

Cheers,
John

IanDG

Hi John

Thanks for the posts.

Following your directions I've found where I was going wrong.  I have what seems to be an unusual breadboard and I hadn't realised one or two of the subtleties of the power and ground connections.

Having made sense of the board the circuit is now behaving as intended.

Thanks to everyone for the encouragement and help.

Kind regards

Ian

johncc

Hey that's great!  Good luck!
John

JimboZA

Quote
an unusual breadboard and I hadn't realised one or two of the subtleties of the power and ground connections


Glad you're sorted.

Might be an idea to post details (make / model, photo, link to manufacturer) of the "unusual breadboard" so as to warn others.

Johannesburg hams call me: ZS6JMB on Highveld rep 145.7875 (-600 & 88.5 tone)
Dr Perry Cox: "Help me to help you, help me to help you...."
Your answer may already be here: https://forum.arduino.cc/index.php?topic=384198.0

IanDG

Hi Jimbo

Sorry I'm not sure where I got it from.  It was purchased as part of a previous attempt to get to grips with electronics - which obviously failed dismally.  The ability to program the Arduino has convinced me to have another go so I got all the bits and pieces I had previously purchased out, one of which was the board, and added the UNO 3.

The board has no manufactures name on it or instruction with it.  I had simply miss-interpreted the markings indicating how the various pin holes are linked.  As soon as I simplified the circuit driving the LED I realised that one of the rails runs across the board despite the graphics that I read as meaning it only ran half way across the board.

It's just a bit embarrassing how long it has taken me to realise how it is wired up; it may be why I had so many problems in my previous attempt at electronics.

Thanks again for yours, Johns and Peters help.

Kind regards

Ian

johncc

Was it one like this?

Code: [Select]
http://www.kpsec.freeuk.com/breadb.htm

Go Up