UTHM idp team 120

Dependencies:   ESP8266 Servo TextLCD mbed

Fork of ACS712HelloWorldDemo by m b

Committer:
tommy1994
Date:
Fri Sep 01 02:44:32 2017 +0000
Revision:
7:2d11e14b5cc4
Parent:
5:a022ca4aaa1e
UTHM idp team 120

Who changed what in which revision?

UserRevisionLine numberNew contents of line
mikeb 0:a35011a2fdaa 1 #include <mbed.h>
nyengele 3:9cae7baf7ccd 2 /** A Hall-Effect sensor for measuring current levels in a given path
nyengele 3:9cae7baf7ccd 3 *
nyengele 3:9cae7baf7ccd 4 * Can be used as a current detector
nyengele 3:9cae7baf7ccd 5 *
nyengele 3:9cae7baf7ccd 6 * Example:
nyengele 3:9cae7baf7ccd 7 * @code
nyengele 3:9cae7baf7ccd 8 * // Periodically read current levels in a circuit and
nyengele 3:9cae7baf7ccd 9 * // send output to PC terminal
nyengele 3:9cae7baf7ccd 10 *
nyengele 3:9cae7baf7ccd 11 * #include "mbed.h"
nyengele 3:9cae7baf7ccd 12 * #include "ACS712.h"
nyengele 3:9cae7baf7ccd 13 *
nyengele 3:9cae7baf7ccd 14 * // Connect the sensor analog output pin to mbed's AnalogIn pin
nyengele 3:9cae7baf7ccd 15 * ACS712 dev(p18);
nyengele 3:9cae7baf7ccd 16 * // Connect mbed to pc's USB port
nyengele 3:9cae7baf7ccd 17 * Serial pc(USBTX, USBRX);
nyengele 3:9cae7baf7ccd 18 *
nyengele 3:9cae7baf7ccd 19 * int main() {
nyengele 3:9cae7baf7ccd 20 * pc.printf("Sensor Log: \n\n\r");
nyengele 3:9cae7baf7ccd 21 * while (1) {
nyengele 3:9cae7baf7ccd 22 * // Read current from sensor and output to pc terminal
nyengele 3:9cae7baf7ccd 23 * pc.printf("Sensor Value: %2.2f A\n\r", dev);
nyengele 3:9cae7baf7ccd 24 * wait(0.200);
nyengele 3:9cae7baf7ccd 25 * }
nyengele 3:9cae7baf7ccd 26 * }
nyengele 3:9cae7baf7ccd 27 * @endcode
nyengele 3:9cae7baf7ccd 28 */
nyengele 3:9cae7baf7ccd 29
mikeb 0:a35011a2fdaa 30 class ACS712 {
mikeb 0:a35011a2fdaa 31
mikeb 0:a35011a2fdaa 32 public:
nyengele 3:9cae7baf7ccd 33 /** Create a hall-effect sensor of the specified type
nyengele 3:9cae7baf7ccd 34 *
nyengele 3:9cae7baf7ccd 35 * @param _pin mbed AnalogIn pin where the analog output of sensor is connected
nyengele 3:9cae7baf7ccd 36 * @param voltDivRatio resistor voltage division ratio at output of the sensor
nyengele 3:9cae7baf7ccd 37 * @param type type of ACS712 sensor used
nyengele 3:9cae7baf7ccd 38 *
nyengele 3:9cae7baf7ccd 39 * @note Supported types of sensors:
nyengele 3:9cae7baf7ccd 40 */
mikeb 0:a35011a2fdaa 41 ACS712(PinName _pin, float voltDivRatio = 1, short type = 5);
mikeb 0:a35011a2fdaa 42
nyengele 3:9cae7baf7ccd 43 /** Read the value of the measured current in amps
nyengele 3:9cae7baf7ccd 44 *
nyengele 3:9cae7baf7ccd 45 * @return current value in amps
nyengele 3:9cae7baf7ccd 46 */
mikeb 0:a35011a2fdaa 47 float read();
nyengele 4:f6c160553ca3 48 ACS712& operator=(const ACS712&);
nyengele 3:9cae7baf7ccd 49
nyengele 3:9cae7baf7ccd 50 /** Read the value of the measured current in amps
nyengele 3:9cae7baf7ccd 51 * Allows the ACS712 object to be used in a float context
nyengele 3:9cae7baf7ccd 52 *
nyengele 3:9cae7baf7ccd 53 * @return current value in amps
nyengele 3:9cae7baf7ccd 54 */
nyengele 1:4f9effb20c29 55 operator float() { return read(); }
mikeb 0:a35011a2fdaa 56
mikeb 0:a35011a2fdaa 57 private:
mikeb 0:a35011a2fdaa 58 AnalogIn sensor;
mikeb 0:a35011a2fdaa 59 float translate(float);
mikeb 0:a35011a2fdaa 60 float ratio;
mikeb 0:a35011a2fdaa 61 short type;
mikeb 0:a35011a2fdaa 62
mikeb 0:a35011a2fdaa 63 };
mikeb 0:a35011a2fdaa 64
nyengele 4:f6c160553ca3 65 ACS712::ACS712(PinName _pin, float voltDivRatio, short sensorType) : sensor(_pin){
mikeb 0:a35011a2fdaa 66 ratio = voltDivRatio;
nyengele 4:f6c160553ca3 67 type = sensorType;
mikeb 0:a35011a2fdaa 68 }
mikeb 0:a35011a2fdaa 69
mikeb 0:a35011a2fdaa 70 float ACS712::translate(float val){
mikeb 0:a35011a2fdaa 71 switch(type){
mikeb 0:a35011a2fdaa 72 case 5:
nyengele 5:a022ca4aaa1e 73 return (val*ratio - 2.46*ratio)/(.185*ratio);
mikeb 0:a35011a2fdaa 74 case 20:
nyengele 5:a022ca4aaa1e 75 return (val*ratio - 2.46*ratio)/(.1*ratio);
mikeb 0:a35011a2fdaa 76 case 30:
nyengele 5:a022ca4aaa1e 77 return (val*ratio - 2.46*ratio)/(.066*ratio);
mikeb 0:a35011a2fdaa 78 default:
mikeb 0:a35011a2fdaa 79 return 999;
mikeb 0:a35011a2fdaa 80 }
mikeb 0:a35011a2fdaa 81 }
mikeb 0:a35011a2fdaa 82
mikeb 0:a35011a2fdaa 83
mikeb 0:a35011a2fdaa 84 float ACS712::read(){
nyengele 1:4f9effb20c29 85 return ACS712::translate(sensor * 3.3);
mikeb 0:a35011a2fdaa 86 }
mikeb 0:a35011a2fdaa 87
nyengele 4:f6c160553ca3 88 ACS712& ACS712::operator=(const ACS712& rhs){
nyengele 1:4f9effb20c29 89 sensor = rhs.sensor;
nyengele 1:4f9effb20c29 90 ratio = rhs.ratio;
nyengele 1:4f9effb20c29 91 type = rhs.type;
nyengele 4:f6c160553ca3 92 return *this;
mikeb 0:a35011a2fdaa 93 }