0% found this document useful (0 votes)
3 views5 pages

NET3001 Tutorial08v2

This tutorial focuses on using timers in Arduino to generate PWM for controlling a servo motor and RGB LEDs. Task 1 involves generating a 50Hz PWM signal using Timer1 to control a servo motor, while Task 2 involves using Timer0 and Timer2 to control the colors of an RGB LED. Students are required to submit their code files for both tasks and work in pairs, noting their partner's name.

Uploaded by

ismailfarah31
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views5 pages

NET3001 Tutorial08v2

This tutorial focuses on using timers in Arduino to generate PWM for controlling a servo motor and RGB LEDs. Task 1 involves generating a 50Hz PWM signal using Timer1 to control a servo motor, while Task 2 involves using Timer0 and Timer2 to control the colors of an RGB LED. Students are required to submit their code files for both tasks and work in pairs, noting their partner's name.

Uploaded by

ismailfarah31
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

NET 3001

Real-time Systems

Tutorial 8 – Timers (PWM)

Marking Scheme:

You can work in pairs. However, you need to submit your own work and include the name of the
student you worked with. Please refer to the lab marking guide indicated in the course outline.

Table of Contents
I. Introduction ...................................................................................................................................... 1
II. Task 1 – Generate PWM with Timer1 Using ICR1 to Control Servo Motor ...................................... 1
III. Task 2 Control RGB LED colours using PWM................................................................................. 3
IV. File Submission.............................................................................................................................. 5

I. Introduction

In this tutorial, you will work with Timers in Arduino to generate PWM to control servo-motor and RGB
LEDs. You should use the starter code provided for task 1 and the class code provided for task 2.

II. Task 1 – Generate PWM with Timer1 Using ICR1 to Control Servo Motor

In class, we used the Timer0 with 1024 prescalar to generate PWM with 61 Hz frequency. That was the
closest that we could get to 50 Hz (the servo-motor required frequency). Also, in the lecture we
indicated that servo-motor SG90 moves from angle -90 to 90 (we can also think of this as 0 to 180). To
set the angle to -90, practically, we need 0.52ms duty cycle period of 50Hz PWM. Then, 1.4ms duty cycle
period of 50Hz PWM will set the angle to 0. Finally, to set the angle to 90, 1.4ms
2 duty cycle period of
50Hz PWM is needed.

To get the specific 50Hz PWM frequency, we need to use the Timer1 in FAST PWM mode using ICR1
register. This is waveform 14 in the datasheet. In this mode, we can load the TOP value that would
generate the desired frequency in the ICR1 register. This will cause the Timer1 to overflow at the value
set in OCR1. Let see how we can calculate the value of OCR1(TOP) for 50Hz PWM using 64 for prescalar.

Frequency of PWM = Frequency of CPU Clock / (Prescalar x (1 + TOP))

(1) TOP = (Frequency of CPU Clock / (Frequency of PWM x Prescalar)) – 1

TOP = (16000000 / (50 x 64)) – 1


TOP = 4999
That means we need to set ICR1 to 4999 to be able to generate a 50Hz PWM using 64 as prescaler. Now
we need to calculate the value of OCR1A for the 0.52ms, 1.4ms, and 2.4ms duty cycles. The first one is
calculated for you. You should calculate the rest.

(1) Timer frequency = CPU frequency / Prescalar

Timer frequency = 16000000 / 64 = 250 kHz

(2) Timer Tick = 1 / Timer Frequency

Timer Tick = 1 / 250 kHz = 4us

(3) OCR1A = Duty cycle / Timer Tick

OCR1A (for -90 degrees) = 0.00052 / 0.000004 = 130

OCR1A (for 0 degrees) = ----------------------------------

OCR1A (for 90 degrees) = -----------------------------------

Now, that we have all the setup information we need, we can setup Timer1 in waveform 14 with
prescalar 64.
You need the following components to create the circuit for this task:

1. (1) x Elegoo Uno R3


2. (1) x SG90 servo-motor
3. (1) Power Supply
4. (1) 9V Battery
5. M-M wires

Create the following circuit:

As mentioned during the lecture, it is safer to use an external power supply module for the servo-motor.
We have an external power supply module in our kit as well as a 9V battery. Please follow the
instruction on page 136 to 138 of Elegoo Super Starter kit pdf and this link: Breadboard Power Supply
Module ([Link]).

Create a new project and add the provided code into your project. There is a skeleton code to setup the
timer. Fill-in the required parts and deploy the project to your Arduino. Connect your Arduino to the
circuit and check the result. If everything works as expected, change the program in a way that
continuously rotates the servo-motor from 0 to 180 degrees with a delay of 20ms per degree.

III. Task 2 Control RGB LED colours using PWM

In this task, you will setup timer0 and timer2 in PWM mode to control an RGB LED. We saw an example
in the class on how to change the brightness of an LED using PWM. The RGB LED that we have in our kit
is a common cathode. That means there is one pin that needs to be connected to GND and the other
three pins correspond to red, green and blue (see the image below) that need to be connected to PWM
pins. You can use the code provided in the class on adjusting brightness of two regular LEDs as a starter
code and add the code for timer2.
You need the following components to create the circuit for this task:

1. (1) x Elegoo Uno R3


2. (1) x RGB LED
3. (3) 220 Ohm resistors
4. M-M wires

Create the following circuit:

As you noticed from the circuit, we are using timer0 channel A for red, timer0 channel B for green, and
timer2 channel B for the blue colour.

Create a project based on the given code and add the required setup for timer2. Also, modify the
setDutyCycle function to accept 3 parameters (one for each colour). Then, call this function in your main
to set and control the colour of RGB LED. Use values between 0 to 128 to see distinct colours.
IV. File Submission

- Submit your .cpp files for task 1 and task 2 into Brightspace by the deadline.
- Ensure that the TAs record your attendance before leaving the lab.

You might also like