Pulse Width Modulation · Using digital pins the Arduino sends a constant 5 volts, which is like...

9
Pulse Width Modulation and analogWrite ( ); 28

Transcript of Pulse Width Modulation · Using digital pins the Arduino sends a constant 5 volts, which is like...

Page 1: Pulse Width Modulation · Using digital pins the Arduino sends a constant 5 volts, which is like only being able to “pedal” constantly without ever coasting. Certain digital pins

Pulse Width Modulation

and

analogWrite ( );

28

Page 2: Pulse Width Modulation · Using digital pins the Arduino sends a constant 5 volts, which is like only being able to “pedal” constantly without ever coasting. Certain digital pins

Set up the Odyssey Board, dowel, and socket block. (Wire clip is optional.)

Your blocks may use any horizontal or vertical dowel holes. They do not have to look exactly like the ones shown.

Materials needed to wire one LED.

● Odyssey Board● 1 dowel● Socket block● Wire clip (optional)● 1 Female to Female (F/F) wire● 1 F/F resistor wire● LED

Note: The color of the wires, LED, and blocks does not matter.

29

Page 3: Pulse Width Modulation · Using digital pins the Arduino sends a constant 5 volts, which is like only being able to “pedal” constantly without ever coasting. Certain digital pins

Pulse Width Modulation - PWM

In this activity, you will learn about a very important coding statement: analogWrite ( );

In previous activities, you used digitalWrite ( );, which can only send and on or off signal. Digital pins 0-13 only have two states, HIGH (5v) and LOW (0v) which means a LED, motor, or other component can only be fully on or off.

What if you need to have the LED at half the brightness, or a motor at half the speed? Pulse-Width Modulation is a tricky way of rapidly turning a digital signal on and off to simulate a range of voltage outputs between 0 and 5 volts. Although we can’t set a digital pin to put out 2.5 volts, we can turn the signal off and on quickly so that it is off half the time and on half the time creating an average voltage output of 2.5 volts. To do this, we will use the analogWrite ( ); statement using Pulse Width Modulation (PWM).

A PWM signal has two main components: a duty cycle and frequency. The duty cycle describes the amount of time the signal is high, or on. The “on time” is called the pulse width and you can change (modulate) that time. The frequency determines how fast the PWM completes a cycle. The main use for PWM is controlling DC motors, but it can be used for other actuators.

30

Page 4: Pulse Width Modulation · Using digital pins the Arduino sends a constant 5 volts, which is like only being able to “pedal” constantly without ever coasting. Certain digital pins

Pulse Width Modulation - Cycling AnalogyCyclist 1, continually pedaling at a constant rate will maintain a constant speed.

Cyclist 2, pedaling at the same rate using a pedaling-coasting pattern will travel at a lower average speed than cyclist 1.

Using digital pins the Arduino sends a constant 5 volts, which is like only being able to “pedal” constantly without ever coasting. Certain digital pins digital allow you to use pulse width modulation to create a pulsing “pedal-coast” pattern, or on-off voltage cycle. The microcontroller can quickly turn the signal on and off giving short bursts of 5 volts. The on time of this square “on-off” pattern is called the duty cycle. The amount of “on time” is called the pulse width. You can change (modulate) the pulse width to get varying values from a digital pin.

31

Page 5: Pulse Width Modulation · Using digital pins the Arduino sends a constant 5 volts, which is like only being able to “pedal” constantly without ever coasting. Certain digital pins

Pulse Width Modulation - How to set the duty cycle.

Note: Only 6 digital pins on the Keyestudio UNO board can use PWM, pins 3, 5, 6, 9, 10, 11.

Programming Syntax: analogWrite (pin, value)

This is a very important function that allows you to vary the brightness of an LED, change the speed of a motor, etc. The function writes a PWM square wave to a digital pin with the specified duty cycle.

Two parameters need to be set:The pin number or name to write the signal to The duty cycle value between 0 and 255.

● A value of 0 is always off● A value of 255 is always on● Any value between 0 and 255 can be

used to vary the duty cycle time.

32

Page 6: Pulse Width Modulation · Using digital pins the Arduino sends a constant 5 volts, which is like only being able to “pedal” constantly without ever coasting. Certain digital pins

Task 1: Observing Pulse Width Modulation.

Always unplug the power when changing your circuits.

1. Wire one LED to pin #9 on the UNO.

2. Type or paste PWM Code 1 into the IDE.

3. Plug in the USB cable and upload the sketch.

4. Change the duty cycle value from 255 to 125 and upload the sketch. Observe the brightness of the LED.

5. Change the duty cycle value several times and observe the changes in the LED.

6. You could also connect a motor and change the duty cycle value to vary the speed of the motor.

PWM Code 1

33

Page 7: Pulse Width Modulation · Using digital pins the Arduino sends a constant 5 volts, which is like only being able to “pedal” constantly without ever coasting. Certain digital pins

Now that you know how to use PWM to program the brightness of the LED, you can program it to fade brighter and dimmer on it’s own.

int led = 9; //Defines, or sets the name led to be used in pin 9 int brightness = 0; //Variable that sets the brightness of the LEDint fadeAmount = 5; //Variable that sets the rate the light will change, or brighten/dim

void setup() { pinMode(led, OUTPUT); //Sets the the pin that is defined by led as an output}

void loop() { analogWrite(led, brightness); //Writes (sends) the varying brightness variable value assigned to the LED brightness = brightness + fadeAmount; //Takes the value of brightness and adds the fade amount

//The following if statement reverses the brightness when the variable reaches a maximum, 255 or a minimum, 0 if (brightness <= 0 || brightness >= 255) { fadeAmount = -fadeAmount; //Changes brightness value to its opposite integer value } delay(30); //Sets the wait time (in milliseconds) before running the loop}

Try adjusting the fade value to see how it affects the LED.34

Page 8: Pulse Width Modulation · Using digital pins the Arduino sends a constant 5 volts, which is like only being able to “pedal” constantly without ever coasting. Certain digital pins

Mood Lamp

1. Set up your Odyssey Board as follows using a resistor for each LED:a. A red LED in pin 9b. A Green LED in pin 10c. A Blue LED in pin 11

2. When you have completed your wiring setup, go to odysseyboard.com Learn tab and get the code.

3. Plug in the USB cable and upload the sketch.

4. Put your paper over the lights and enjoy your mood lamp

Challenge: Try to make the lights cycle through the colors of the rainbow rather than between random colors.

You try it now.

Take a piece of white paper and roll it into a cylinder large enough to fit over the socket block and tape the edges. When you finish setting up the LEDs, place the cylinder over the LEDs.

35

Page 9: Pulse Width Modulation · Using digital pins the Arduino sends a constant 5 volts, which is like only being able to “pedal” constantly without ever coasting. Certain digital pins

Candle

1. Set up your Odyssey Board with○ 1 yellow LED in pin 11 ○ 1 red LED in pin 10○ 1 yellow LED in pin 9○ (Use a resistor for each LED.)

2. When you have completed your wiring, type in the code to the right. It is good practice to add the // and your own programmer comments for each statement. You can also get the code from odysseyboard.com/Learn.

3. Plug in the USB cable and upload the sketch.

4. Slide your paper over the lights, and enjoy your candle.

You try it now.

Take a piece of white paper, roll it into a cylinder large enough to slip over your socket block, and then tape the edges.

//LED Candle

int yellowLed1 = 9;int redLed = 10;int yellowLed2 = 11;

void setup(){ pinMode(yellowLed1, OUTPUT); pinMode(redLed, OUTPUT); pinMode(yellowLed2, OUTPUT);}

void loop(){analogWrite(yellowLed1, random(120)+135);analogWrite(redLed, random(120)+135);analogWrite(yellowLed2, random(120)+135);delay(random(100));}

36