Pages: [1]   Go Down
Author Topic: LSR sketch  (Read 1611 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Karma: 0
Posts: 28
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hello. I am going through the GETTING STARTED WITH ARDUINO book and there is an example using a Light Sensitive Resistor and inputing that info into an analog pin to vary an LED brightness.

When I do the sketch, the LED only varies slightly. I would like it to go all the way out and back up if possible.

Here is the code Im using, directly from the book:

// Example 06B: Set the brightness of LED to

// a brightness specified by the

// value of the analogue input

const int LED = 10;

int val = 0;

// the pin for the LED

// variable used to store the value

// coming from the sensor

void setup() {

pinMode(LED, OUTPUT); // LED is as an OUTPUT

// Note: Analogue pins are

// automatically set as inputs


void loop() {

val = analogRead(0); // read the value from

analogWrite(LED, val/4); // turn the LED on at

// the sensor

// the brightness set

// by the sensor

delay(10); // stop the program for


// some time


Offline Offline
God Member
Karma: 39
Posts: 988
Get Bitlash:
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

It seems the range of your input values may not be driving the full range of output values.

Here's something I'd try.  First, do an experiment: print out "val" and see what values it assumes for the range of light your application needs.  Read up on the map() command in the reference and use map() to map the input range to the output range 0..255 -- in place of analogWrite(…, val/4);

Good luck with your project,



Global Moderator
Offline Offline
Brattain Member
Karma: 506
Posts: 19120
Lua rocks!
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

@sokbok: Please edit your post, select the code, and put it between [code] ... [/code] tags.

You can do that by hitting the # button above the posting area.

Also don't double-space your code like that, it isn't an essay.

Pages: [1]   Go Up
Jump to: