ADC Interface with Ultrasonic Proximity Sensor

From Embedded Systems Learning Academy
Revision as of 17:09, 6 November 2013 by Preet (talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

Overview

The LV-MaxSonar-EZ0 ultrasonic sensor interfaces with your microcontroller using an ADC interface. The microcontroller on your development board is equipped with an analog-to-digital converter. This converts an analog signal (continuous voltage that varies over time) into corresponding digital values (discrete integers that approximate the input voltage). For example, let’s say that a 12-bit ADC operates on 3.3V, which is the case for your SJSU One development board. This means that a voltage range between 0V and 3.3V can be divided into 212 or 4096 discrete steps. If the ADC is returning an integer value of 2048, you can calculate that a sensor connected to the ADC is outputting a voltage of 1.65 volts.

Pin Connections

The ultrasonic sensor has 7 pins. We will connect 3 of them to your board:

  1. Use a red wire to connect Pin “+5” on the sensor to Pin “3.3V” on your board.
  2. Use a black wire to connect Pin “GND” on the sensor to Pin “Gnd” on your board.
  3. Use another color wire to connect Pin “AN” on the sensor to Pin “A0-4” on your board.

Source Code Description

Add #include “adc0.h” to the top of your main.c. This gives you access to the ADC functions.

Your microcontroller has 80 pins but many more features. Therefore, one pin can do many different things. We will need to tell it to internally connect pin P1.30 to the ADC hardware as opposed to some other hardware. To do this we will need to write the appropriate value to a Pin Select Register:

LPC_PINCON->PINSEL3 |= (3 << 28);

Now, you can read values from the ADC with a call to:

adc0_get_reading(4);

The “4” represents that we are reading values from ADC channel 4. Your board has multiple ADC channels. For example, the light sensor is connected to channel 2, so we can’t use that one.

Sample Code

#include “adc0.h”

int main(void)
{
    int reading = 0;

    // Initialization :
    LPC_PINCON->PINSEL3 |=  (3 << 28); // ADC-4 is on P1.30, select this as ADC0.4

    while(1) {
        reading = adc0_get_reading(4); // Read current value of ADC-4
        printf("\nADC Reading: %d", reading);        
        delay_ms(1000);
    }

    return 0;
}

Remaining Work

Your job is to turn the integer values you receive from ADC into numbers that are more useful. Do this by converting the ADC value into a voltage value. Next, convert the voltage value into a distance measurement value. The conversion rate needed to convert voltage to inches/centimeters is in the sensor data sheet, which I will leave for you to find.

Adding a second ADC type sensor

It is possible to connect a second sensor that uses ADC to your board using ADC Channel 5. This will require a different pin select code so ask a TA for assistance.