Go Down

Topic: Dim LED with level Sensor (Read 1 time) previous topic - next topic


Hi everybody,

Just get an Arduino and enjoyed it so much. My pleasure for tonight, my first post on the Arduino family forum.

I'm trying to dim an Led according to the circuit explain below:

LED connected to pin 9 and have a 560 ohms resistor before ground
Sensor is connected to 5V pin, and have a 10Kohms resistor before ground, and a connection to analog pin 0

Below the specs of my level sensor:

Power requirements: Up to +5 VDC
Communication: Resistive Output
Sensor Output: 550 ohms empty, 60 ohms full, ± 20%
Resistance Gradient: 40 /inch (16 /cm), ± 20%

I've tried to changed the value of the resistor connected to my sensor, but nothing is happeing, brightness is still fix.

Below is the code I've used:

[glow]//Level Sensor Pin
int levelPin = 0; //the analog pin the level sensor is
                 //connected to
                 //the level sensor is not calibrated to any units so
                 //this is simply a raw sensor value (relative level)
//LED Pin
int ledPin = 9;   //the pin the LED is connected to
                 //we are controlling brightness so
                 //we use one of the PWM (pulse width
                 // modulation pins)
void setup()
 pinMode(ledPin, OUTPUT); //sets the led pin to output
* loop() - this function will start after setup
* finishes and then repeat
void loop()
int waterLevel = analogRead(levelPin); //Read the
                                       // waterlevel
waterLevel = map(waterLevel, 0, 900, 0, 255);
        //adjust the value 0 to 900 to
        //span 0 to 255
waterLevel = constrain(waterLevel, 0, 255);//make sure the
                                          //value is betwween
                                          //0 and 255
analogWrite(ledPin, waterLevel); //write the value

I'm a novice in programming and electronic, so I'm sure I did it wrong.
I'm still trying different programs, and circuits, but start to be a little bit afraid of what could happen (an explosion maybe ??  ;) )

I'm more than open to any suggestion, even if you have to tell me that I'm a dum, as I will agree.

Thanks a lot in advance,
I'm going back to my water circuit  :D



Add a Serial.begin() statement to setup, and Serial.print() and/or Serial.println() statements to loop, to echo the value read from the sensor, the value after the mapping, and the value after the constrain function.

Also, please describe how the level sensor is connected to the Arduino.


Hi Paul,

I've used the scheme which is in a beginner exercise to dim an LED with a photosensor.

you can see that ircuit on that website:

I've just replaced the photosensor with my level sensor.

I've used the same programm as it was working really well with the photosensor.

I also had a reply from the company whose making the sensor and they said maybe it's the value of the resistor which is a problem.

I'm lost but again, still trying...


You haven't explained HOW the level sensor is connected. The analog pins are for measuring voltage, not resistance. Knowing the resistance of the sensor at each extreme is necessary to choosing an appropriate resistor to form a voltage divider, to maximize the range of values returned by the ADC.

It is still necessary to connect the resistor and sensor to the Arduino correctly. It isn't clear that you have done that.

You could also add Serial.print() statements to loop() (and Serial.begin() to setup()) to see what actual data you are getting from the analog pin.


Dear Paul,

Thank you for your answer. Basically, I'm trying to measure value of V according to how it's changing due to the sensor resistance. But Again I'm a novice and I've done it the wrong way.

What I'm aiming is the change of the resistance value of the sensor and then change it as a value I can use to change LED brightness.

I thought the photosensor was also acting as a resistor, so its action was exactly the same as my level sensor, that's why I ve just exchange both.

Is the circuit I'm using as a reference is not the good one ?


Is the circuit I'm using as a reference is not the good one ?

It might be. In fact, it probably is.

But, the steps that you have taken to implement that circuit may not have been correct. So, you need to forget about the circuit for a moment, and describe what you have connected to what.

Like this:
I have a xx ohm resistor connected to +5V and analog pin x. I also have the sensor connected to analog pin and ground.

Or, if you have them connected in reverse order, that's fine, too.

But, we don't know how you have tried to follow the circuit.

We also don't know what values you are seeing in the Serial monitor after having added the statements I suggested.

Perhaps as you suggested earlier, you are simply using too large a resistor in the voltage divider circuit, so that the range of values read from the analog pin is too small to have a noticeable affect on the LED brightness. In which case you can either try a different resistor or change the mapping statement in the code.

To help you, though, you need to quit saying that you followed the circuit exactly, and say exactly what you did, and what values you are now seeing.


Hi Paul,

Following your advice, I've tried to understand how to use the 'serial' function, but all tutorials are explaining different things so I'm not really sure about what to add exactly into the code.

I will try to experiment this overnight, and will let you know the result, ... if I have one  :-/

Anyway, I have to say, having problems is a really good way to discover things.

Thanks again.


Dear Paul,

Sorry to come back to you so late  :(
Processing is fantastic, from that I was able to adjust the map value and make the sensor work.
Next step, being able to connect it to a Velleman K8064 to dim a 12V halogen bulb.
So exciting...
Will keep you updated and thank you so much for your advice.

Go Up