m b
/
ACS712HelloWorldDemo
Demo of the ACS712 Library
Fork of ece4180_lab4_ACS712 by
ACS712.h
- Committer:
- nyengele
- Date:
- 2016-03-13
- Revision:
- 3:9cae7baf7ccd
- Parent:
- 2:2b5233355986
- Child:
- 4:f6c160553ca3
File content as of revision 3:9cae7baf7ccd:
#include <mbed.h> /** A Hall-Effect sensor for measuring current levels in a given path * * Can be used as a current detector * * Example: * @code * // Periodically read current levels in a circuit and * // send output to PC terminal * * #include "mbed.h" * #include "ACS712.h" * * // Connect the sensor analog output pin to mbed's AnalogIn pin * ACS712 dev(p18); * // Connect mbed to pc's USB port * Serial pc(USBTX, USBRX); * * int main() { * pc.printf("Sensor Log: \n\n\r"); * while (1) { * // Read current from sensor and output to pc terminal * pc.printf("Sensor Value: %2.2f A\n\r", dev); * wait(0.200); * } * } * @endcode */ class ACS712 { public: /** Create a hall-effect sensor of the specified type * * @param _pin mbed AnalogIn pin where the analog output of sensor is connected * @param voltDivRatio resistor voltage division ratio at output of the sensor * @param type type of ACS712 sensor used * * @note Supported types of sensors: */ ACS712(PinName _pin, float voltDivRatio = 1, short type = 5); /** Read the value of the measured current in amps * * @return current value in amps */ float read(); float operator=(ACS712&); /** Read the value of the measured current in amps * Allows the ACS712 object to be used in a float context * * @return current value in amps */ operator float() { return read(); } private: AnalogIn sensor; float translate(float); float ratio; short type; }; ACS712::ACS712(PinName _pin, float voltDivRatio, short type) : sensor(_pin){ ratio = voltDivRatio; this.type = type; } float ACS712::translate(float val){ switch(type){ case 5: return (val*ratio - 2.5*ratio)/(.185*ratio); break; case 20: return (val*ratio - 2.5*ratio)/(.1*ratio); break; case 30: return (val*ratio - 2.5*ratio)/(.066*ratio); break; default: return 999; break; } } float ACS712::read(){ return ACS712::translate(sensor * 3.3); } ACS712& ACS712::operator=(ACS712& rhs){ sensor = rhs.sensor; ratio = rhs.ratio; type = rhs.type; return this; }