Control a led by a photoresistor connected to an analog input of an arduino mega 2560 carte?

Hello friends,

I have a project of which a subsystem is automatic lighting:
To control a led by a photoresistor connected to an analog input of an arduino mega 2560 board, I created this program but when I simulate it on the Proteus isis environment, the voltage detected always remains at 5V even if I change the 'light intensity. And suddenly the led does not light up.
But when I test it with a UNO card it works very well.
I don't know in which level I have a problem is that at the level of the code or the pinout on isis.

please i need the help.
Thank you.
arduino code:

//détecter la présence ou l'absence de lumière par une photorésistance
//dont sa valeur lue et comparée au seuil afin de décider si on allume la led ou non

const int photoR=A0;
const int led=10;
int valeur_lue;
float tension, seuil=2.5;

void setup() {


void loop() {

tension=map(valeur_lue,0,1023,0,5000)/1000.0; //tension correspondant en Volt

if(tension>seuil){ //c'est la journée (présence de lumière)
digitalWrite(led,LOW); //on Ă©tient la led
}else{ //c'est le soir (absence de lumière)
digitalWrite(led,HIGH); //on allume la led
Serial.print("la valeur lue: ");
Serial.print("la tension: ");

Your analog pin has to be setting like input in Setup. Maybe it is your problem. You only have Led like output and your Serial.

void setup() {

1 Like

So that tells you what many people here already know. Simulators are not perfect at doing what actual hardware would do.

1 Like

Yes, I had seen this problem when I was working with a simulator. There often you have to set the analog pins as inputs into your program for put ON your ADC. It isn't necesary when you upload it to your Arduino but when you are working with a simulator often it could be a mistake.

When you say "photoresistor" do you mean an LDR?
LDRs are OK to a point, but their spectral sensitivity may not always match what you need for a lighting project. I guess you want to system to control light for humans.
This is just a side idea, but I tend to use an ambient light sensor instead of and LDR because they are tuned to match the human vision spectrum.
For example, the TEPT4400 has peak sensitivity of 570nm which closely matches the human eye - hence very suitable for lighting control projects.
Hope this helps.

1 Like

:thinking: i will try
thank you

thank you so mush :slight_smile:

I tried to add pinMode (photoR, INPUT), but still the 5V value remains unchangeable even if I change the light intensity

The pinMode doesn’t need to be set when using an analogue input, so it doesn’t matter what you set it to, and you don’t appear to use it anyway.

Try using


The other thing you can try is reducing R2 to 1K.

1 Like

ok, thnx