Lab 4

Dependencies:   mbed

Committer:
mikeb
Date:
Mon Mar 14 13:44:12 2016 +0000
Revision:
1:f68acb38a472
Parent:
0:a35011a2fdaa
Child:
2:ac894e4cff0d
Final Version

Who changed what in which revision?

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