PV198 Study Materials

Preliminaries

Theory

- Introduction

- GPIO

- Interrupts

- Timer

- PWM

- ADC

- Communication buses

- SPI

- I2C

- UART

Practical

PWM

PWM (Pulse Width Modulation) is a technique to have gradual control in digital context. The idea is to use proper change in time to achieve an illusion of gradual control.

An example situation is the control of LED light, we can turn the light ON and OFF, but how to achieve 50% brightness with that? We can periodically turn the led ON and OFF, if we do that in a 1:1 ratio at 1s, we will have a blinking light. However, if we increase that frequency high enough, we will fool our eyes into perceiving the LED as dimmed light compared to the ON state.

This is the basis of PWM - the system switches between LOW and HIGH voltage states with a fast enough frequency. We achieve gradual control by varying the ratio between the states.

PWM pulse intro

We will use the picture to define basic terminology:

In the picture period, we have a static period with a duty cycle of 25% in the first two pulses and 50% in the third pulse.

Assuming an ideal situation, that would result in the LED being dimmed to 25% of its brightness or 50% respectively. But that’s not the case because of the way the LED works, however, in this course we will assume that it is true.

The same technique can be used for other compatible systems: (Not everything behaves nicely when switched ON/OFF at high frequency)

Types

There are multiple ways we can set up the pulse. They are beneficial in various situations, for this course, we will use only the edge aligned approach, however, it is good to know that there are alternatives.

Edge aligned

PWM edge aligned

The signal gets to 1 at the beginning of the pulse and switches to 0 once the duty cycle was fulfilled.

Center aligned

PWM center aligned

Same as edge aligned mode, but every second period is mirrored - starts at 0 and switches to 1 at the end of pulse to achieve duty cycle.

Combined

PWM center aligned

Combines two PWM pulses into one, first one is used to decide when the value switches from 0 to 1, second one value is used to decide when the value goes from 1 to 0.

Communication

Instead of direct control, PWM can also be used as a communication mechanism.

RC Servo

RC Servo is a device that can rotate the output shaft to a certain angle, that angle is only input for the servo. The angle is provided with PWM pulses. The servo expects an edge aligned pulse at a frequency of 50Hz. Instead of the duty cycle, the system specifies the input as a duration of the segment with 1 in the pulse.

The input should be within a 1-2ms duration, where 1ms is the minimal angle and 2ms is the maximum angle. Any length between that is linearly interpolated between those two values, however, most servomotors are limited by being able to detect only up to 512 values.

In case the movement range of servo is 180 degrees, 1ms sets servo to 0 degrees, 1.5ms sets servo to 90 degrees, and 2ms sets servo to 180 degrees. It’s up to us how exactly we interpret the meaning of the signal in our code, in some scenarios it’s practical to work with -90...90 range instead of 0...180.

Sonar

Sonar is a device that gives us a guess about how far away is something in front of the sonar. (Really unprecise device) The principle behind is that the sonar creates a sound wave and measures how long it took for the wave to return.

The way some sonars work is that they provide a digital pulse as output. The length of that pulse tells us the distance from the obstacle. The motivation is that this mirrors the inner workings of the sonar: Once a sound pulse is sent, the output switches from 0 to 1, and once that sound pulse returns, the output switches from 1 to 0.