2
\$\begingroup\$

I just started looking into electronics and circuits. I purchased the Arduino Starter Kit to help get me started. I have a question regarding one of the 15 projects it comes with. Specifically with project 4. Basically the the project introduces photoresistors, in this case 3, and the multi light LED, which I believe is a standard RGB LED.

The part I don't understand is that we have to make a calculation for the resistor readings. We have to convert the reading, which would be in a range of 0-1023 to a range of 0-255 for the analogWrite() function. The calculation is very simple, just divide the reading by 4 and you get a measurement within the 0-255 range.

I don't understand the need for the conversion. What is the need for it? Why is the sensor range 0-1023 and not some other range? All I get from the reading is that it is needed for the function and doesn't go into any more detail other than that. I'm sure it is something fundamental that I am just not seeing.

Any point in the right direction would be appreciated. Thanks!

\$\endgroup\$

1 Answer 1

2
\$\begingroup\$

If you look at the datasheet for the MCU at the center of the Arduino, the ATmega328, you'll see that it has 10-bit ADCs and 8-bit PWM (some can be 9- or 10-bit, but the Arduino libraries restrict them to 8).

Dividing the 10-bit ADC result (0 to 1023, 210-1) by 4 (22) scales it down to a 8-bit value (0 to 255, 28-1), which will fit in the PWM registers.

\$\endgroup\$
1
  • \$\begingroup\$ Ahh ok interesting. I looked up ATmega328 looks like I have a ton of learning to do. I don't fully ADC but it seems to make sense. Thanks \$\endgroup\$ Feb 24, 2014 at 0:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.