Embedded system Fun Blog
























































Find out all the best information, libraries and circuit about the latest Embedded systems.
Showing posts with label ez1. Show all posts
Showing posts with label ez1. Show all posts

Saturday, 7 January 2012

MBED Example: How to read MaxSonar EZ1 using mbed AnalogIn

.from: http://mbed.org/users/shimniok/programs/MaxSonar_EZ1_Analog/llju06


#include "mbed.h"

// MaxSonar EZ1 test program
// by Michael Shimniok http://www.bot-thoughts.com/
//
// Based on datasheet here: http://www.maxbotix.com/uploads/LV-MaxSonar-EZ1-Datasheet.pdf
// Reads from AN (analog) pin connected to mbed p20, assumes 3.3V supply to EZ1 module.
//
// mbed -> EZ1
// -----------
// VOUT -> +5
// GND  -> GND
// p20  -> AN
//

AnalogIn ain(p20);
Serial pc(USBTX, USBRX); // tx, rx

int main() {
    float adc, volts, inches;
    int feet, in;
   
    pc.baud(115200);
   
    while (1){
        adc = ain.read();           // read analog as a float
        volts = adc * 3.3;          // convert to volts
        inches = volts / 0.0064;    // 3.3V supply: 6.4mV per inch
        feet = (int) inches / 12;   // inches to feet (trunc)
        in = (int) inches % 12;     // remainder -> in(ches)
       
        pc.printf("%8.2f adc %8.2fV %8.2f in %d'%d\"\n", adc, volts, inches, feet, in);

        wait(0.05);                 // 20Hz update rate ; note we aren't truly synchronized to the device or anything...  
    }
}