Thursday, April 17, 2014

Circuit #6 Photo Resistor Notes

Goal: Use a photo resistor (light sensor) to control the brightness of an LED.

PWM - Pulse Width Modulation. The Arduino uses PWM to fake an output analog voltage. It does this by blinking a pin on and off as fast as 1000 times per second. PWM goes one step further by varying the amount of time that the blinking pin spends HIGH vs. LOW. If mainly HIGH, LED appears bright. If mainly LOW, LED appears dim.

map( ) - Will change one range of values into another range. If we tell map( ) our "from" range is 0-1023, and our "to" range is 0-255, map( ) will squeeze the larger range into the smaller range. ie. lightLevel = map(lightLevel, 0, 1023, 0, 255);

constrain( ) - map( ) could still return numbers outside the "to" range (if they're outside the "from" range), so constrain( ) will "clip" the numbers into a given range. If above the range, resets the highest number to the highest number in the range. If below the range, resets the lowest number to the lowest number in the range. If in range, stays in range. ie. lightLevel = constrain(lightLevel, 0, 255);

analogWrite( ) - Writes an analog value (PWM wave) to a pin. Can be used to light an LED at varying brightness or to drive a motor at various speeds. After a call to analogWrite( ), the pin will generate a steady square wave of the specified duty cycle until the next call to analogWrite( ) (or a call to digitalWrite( ) or digitalRead( ) on the same pin).
Frequency of PWM: ~490 Hz.
On the Uno, pins 5 and 6 have frequency ~980 Hz.
You do not need to call pinMode( ) to set the pin as an output before calling analogWrite( ).
analogWrite( ) has nothing to do with the analog pins or analogRead.
analogWrite(pin, value)

  • pin - Pin to write to 
  • value - Duty cycle between 0 (always off) and 255 (always on)

analogRead( ) - Reads the value from the specified analog pin. The Arduino board contains a 6 channel, 10 bit analog to digital converter. This means it will map input voltages between 0 and 5 volts into integer values between 0 and 1023. This yields a resolution between readings of 5 volts/1024 units, or 0.0049 volts (4.9 mV) per unit. The input range and resolution can be changed using analogReference( ). It takes about 100 microseconds (0.0001 s) to read an analog input, so the max reading rate is about 10000 times per second.

  • pin - Number of the analog input to read from
  • Returns - int(0 to 1023) 

Vilros Ultimate Starter Kit Tutorial Sketches 1-12

No comments:

Post a Comment