## Exercise 2 - Pulse Width Modulation (PWM)

Pulse width modulation is a modulation technique used to encode a message into a pulsing signal. Even though this is a modulation technique for encoding information for transmission, it is widely used in controlling the brightness of an LED and the speed of a motor.

A PWM signal consists of two main components that define its behavior: frequency and duty cycle. The frequency determines how fast the PWM completes a cycle (i.e. 1000 Hz frequency would be 1000 cycles per second). The duty cycle describes the amount of on-time as a percentage of the total time of a cycle. In digital electronics, when a signal is at 5V, we call this ON, and when a signal is at 0V, we call this OFF. From the above image, the first signal is shown as 10% duty cycle, which means its ON time is 10% of a cycle and its OFF time is 90% of a cycle. The second signal is 30% duty cycle because its ON time is 30% of a cycle and its OFF time is 70% of a cycle. A 100% duty cycle means the signal is ON all the time.

By varying the duty cycle of a signal, we can control the percentage of ON time for a circuit, hence indirectly control, for example, the brightness of an LED. 100% duty cycle will have the LED turned ON in full brightness and a 50% duty cycle will have the LED turned ON in half brightness.

Not every PIN on the STEMTera™ Breadboard can be used for PWM. Only PIN with a label ~ can perform as a PWM pin. PIN 3, 5, 6, 9, 10 and 11 only.

For this exercise we will be using the same circuit as the one we used for exercise 1. If you have already done exercise 1, just skip to Write and Upload section.    x1 x1 x2
1. Connect a JUMPER WIRE to GND and 33J.
2. Connect a JUMPER WIRE to PIN3 and 38J.
3. Connect a 220Ω RESISTOR to 34H and 38H.
4. Connect the anode (longer) pin of the RED LED to 34G and cathode (shorter) pin to 33G.  ### Write and Upload the PWM Sketch

In order to execute the sketch for this exercise, launch Arduino IDE then

Click File -> New.

A new sketch editing window will appear. Copy the whole block of code below and paste it into the new sketch window, overwriting the empty `setup()` and `loop()` that was pre-created. Ensure that the board selected is Arduino Uno and the right COM port is chosen according to “Board Selection” chapter.

``````void setup() {
pinMode(3, OUTPUT);
}

void loop() {
analogWrite(3, 255);  // 255 is 100% duty cycle
delay(200);           // wait for 100ms
analogWrite(3, 127);  // 127 is 50% duty cycle
delay(200);           // wait for 100ms
analogWrite(3, 64);   // 64 is 25% duty cycle
delay(200);           // wait for 100ms
analogWrite(3, 32);   // 32 is 12.5% duty cycle
delay(200);           // wait for 100ms
analogWrite(3, 16);   // 16 is 6.25% duty cycle
delay(200);           // wait for 100ms
analogWrite(3, 0);    // 0 is 0% duty cycle
delay(200);           // wait for 100ms
}
``````

Click Upload, and the Arduino IDE will prompt you to Save sketch folder as. Enter `Excercise_2` into the filename field and click save. Once saved, the Arduino IDE will start to compile the sketch. After the compilation is completed, the Arduino IDE will start to upload the compiled sketch into the STEMTera™ Breadboard. During this stage both yellow LEDs on the STEMTera™ Breadboard will start blinking indicating the Arduino IDE is uploading the sketch to the STEMTera™ Breadboard.

### Understanding the PWM Sketch

From the circuit, we know that PIN3 of the STEMTera™ Breadboard is connected to the 220Ω resistor and the Red LED. In order to turn on the LED, we need to instruct the STEMTera™ Breadboard to set PIN3 to OUTPUT in the `setup()` function.

`pinMode(3, OUTPUT);`

The `pinMode()` function shown above takes two parameters. The first parameter is the pin number and the second parameter is the mode of the pin. The above code instructs the STEMTera™ Breadboard to set PIN3 as OUTPUT.

Once PIN3 is configured as OUTPUT, we can then use `analogWrite()` as a PWM pin. For example

`analogWrite(3, 127);`

The `analogWrite()` function shown above takes two parameters. The first parameter is the pin number and the second parameter is the duty cycle from 0 to 255 representing 0% to 100% duty cycle. In the example, the value 127 translate to 50% duty cycle.

In the sketch, we set the PWM from 100% duty cycle to 0% duty cycle in half step with a 200ms delay between each step in a loop.

When PWM, DELAY, PWM, DELAY are executed in a loop, the result of this exercise is the brightness of the LED dimming from full brightness to off and repeat.