Saturday, March 16, 2013

Gizduino Lesson 3 dimmer light

Using the analog input and output pins you can control how much voltage the Gizduino produces to a pin. Having this control of voltage in the Gizduino allows you to vary the brightness of a LED or the speed of a motor.

You will need the following:

 











Schematic Diagram:










 Connect positive end of LED to pin 9 and negative end of LED to a 100 ohm resistor which is connected to the ground pin. Connect the first terminal of the potentiometer to the 5V power pin, the middle terminal to analog input pin 0, and the remaining terminal to the ground pin. 






 
Sketch, Compile, then Upload:

Copy the program below to your Arduino IDE

int ledPin = 9;
int potentiometer;
int brightness;

void setup()
{
  pinMode(ledPin, OUTPUT);
}

void loop()
{
  potentiometer = analogRead(0);

  brightness = potentiometer/4;

  analogWrite(ledPin, brightness);
}

Code Overview:

potentiometer = analogRead(0);

The analogRead function reads the voltage applied to an analog input pin. It reads the voltage between 0V and 5V, which is represented by a number between 0 and 1023. The analog input pin does not need to be specified in the void setup, it is already understood by the Gizduino as input. It is named as “potentiometer” for easier use later on the program.


analogWrite(ledPin, brightness);

Changes the PWM rate of an analog pin; the pin may only be, pins 3, 5, 6, 9, 10, and 11. Using Pulse Width Modulation (PWM) is an effecient way of controlling the amount of voltage entered to the analog pin. Having control of the voltage in the pin allows you to manipulate the response of the pin. For example, you have a LED connected to a analog pin, you control it’s brightness with the use of PWM. Controlling the PWM makes use of the number between 0 and 255, which represents the voltage between 0V and 5V.

The analogWrite function is followed by two settings, the pin number and the PWM rate. Analog pin 9 was used, previously labeled as “ledPin”. “brightness” is the value of the PWM rate, it may be any number between 0 and 255 which represents the voltage between 0V and 5V. In this program the value of ”brightness” varies with the data received from the analog input; the exact value of ”brightness” will be explained later on.

The brightness of the LED will depend on the PWM rate; a maximum PWM rate of 255 will result the LED to emit the brightest, a PWM rate of 0 will result the LED to turn off. If you want to dim the LED, adjust the PWM rate to any value between 0 and 255; for example, if you want to dim the LED by 50%, adjust the PWM rate to 128, which is half of the maximum PWM rate of 255.


brightness = potentiometer/4;

Since the potentiometer reads a value between 0 and 1023, modifications to this variable is needed to for it to be consistent with the PWM rate. In order to do so, arithmetic is used.The range of values the potentiometer can read is 4 times greater than the range of a PWM rate. The potentiometer value is divided by four for it to be reduced to a range from 0 to 255, which will be consistent with the range of PWM rate. Whenever the potentiomer reading is at a maximum of 1023 for example, it will be reduced to 255 which is the maximum of a PWM rate.


You can also experiment by using other sensors such as photoresistors instead of a potentiometer. 

 

No comments:

Post a Comment