Analogue IO

Analogue IO.

  1. Analogue-to-Digital Convertor (ADC)
  2. Analogue Output — PWM (Pulse Width Modulation)
  3. Analogue Output — Frequency Output

Analogue-to-Digital Convertor (ADC)

The Board has two potentiometers (Analogue inputs). These can be read using the ADC (Analogue-Digital-Convertor).

The repository s3.1 illustrates how these can be read.

The output from the library is scaled into a 0…1 range.
We can recover the actual value read using a knowledge of the hardware that generated the signal.

  1. The potentiometers are connected between 0V and 3.3V, the slider is the input to the ADC.
    • Thus the input voltage to the ADC will be between 0V and 3.3V.
  2. The ADC reference voltage is 3.3V.
    • The ADC reads 0 for 0V and 1.0f for 3.3V
  3. The ADC uses 16 bits for the conversion.
  1. Rewrite the code to display the read voltage
  2. How small a voltage change can the ADC read? The full scale 0…3.3V is quantised into 16bits 0x00000xFFFF.
  3. What angle does this correspond to for the potentiometer? If we wanted to measure angles?

Analogue Output — PWM (Pulse Width Modulation)

One form of Analogue output is Pulse-Width-Modulation (PWM). This is often used for motor speed control or as a dimmer mode for controlling LEDs.

Repository s3.2 shows an example of PWM used to control an LED’s brightness.

The example uses a simple ramp

        for( duty=0 ; duty<1.0 ; duty+=0.01 ) {
            red.write(duty);
            wait_ms(100);
        }

A more complex algorithm uses:

        for( duty=0 ; duty<4.6 ; duty+=0.23 ) {
            float brightness = exp(duty)/100;
            red.write(brightness);
            wait_ms(100);
        }

What do you observe as different about the two effects [ hint ]?
(you may want to play about with the delays and values)

Play about with different functions to control the brightness

Only the Red and Green LED on the application shield can be controlled using a PWM output.

Analogue Output — Frequency Output

We can use the PWM output in another way for another form of analogue output. Here we are going to use a fixed 50% duty cycle, equal times on and off. We will vary the period (total time of on and off).

Example s3.3 scans through a frequency range.

        for (f=20.0; f<20.0e3; f+=100) {
            speaker.period(1.0/f);
            ...
        }

What are the lowest and highest frequencies you can hear?

Look at the table of piano note frequencies.


© 2017   Dr Alun Moon
alun.moon@northumbria.ac.uk