m b
/
ACS712
Lab 4
Embed:
(wiki syntax)
Show/hide line numbers
ACS712.h
00001 #include <mbed.h> 00002 /** A Hall-Effect sensor for measuring current levels in a given path 00003 * 00004 * Can be used as a current detector 00005 * 00006 * Example: 00007 * @code 00008 * // Periodically read current levels in a circuit and 00009 * // send output to PC terminal 00010 * 00011 * #include "mbed.h" 00012 * #include "ACS712.h" 00013 * 00014 * // Connect the sensor analog output pin to mbed's AnalogIn pin 00015 * ACS712 dev(p18); 00016 * // Connect mbed to pc's USB port 00017 * Serial pc(USBTX, USBRX); 00018 * 00019 * int main() { 00020 * pc.printf("Sensor Log: \n\n\r"); 00021 * while (1) { 00022 * // Read current from sensor and output to pc terminal 00023 * pc.printf("Sensor Value: %2.2f A\n\r", dev); 00024 * wait(0.200); 00025 * } 00026 * } 00027 * @endcode 00028 */ 00029 00030 class ACS712 { 00031 00032 public: 00033 /** Create a hall-effect sensor of the specified type 00034 * 00035 * @param _pin mbed AnalogIn pin where the analog output of sensor is connected 00036 * @param voltDivRatio resistor voltage division ratio at output of the sensor 00037 * @param type type of ACS712 sensor used 00038 * 00039 * @note Supported types of sensors: 00040 */ 00041 ACS712(PinName _pin, float voltDivRatio = 1, short type = 5); 00042 00043 /** Read the value of the measured current in amps 00044 * 00045 * @return current value in amps 00046 */ 00047 float read(); 00048 00049 00050 /** Read the value of the measured current in amps 00051 * Allows the ACS712 object to be used in a float context 00052 * 00053 * @return current value in amps 00054 */ 00055 operator float() { return read(); 00056 00057 00058 ACS712& operator=(const ACS712&); 00059 } 00060 00061 private: 00062 AnalogIn sensor; 00063 float translate(float); 00064 float ratio; 00065 short type; 00066 00067 }; 00068 00069 ACS712::ACS712(PinName _pin, float voltDivRatio, short sensorType) : sensor(_pin){ 00070 ratio = voltDivRatio; 00071 type = sensorType; 00072 } 00073 00074 float ACS712::translate(float val){ 00075 switch(type){ 00076 case 5: 00077 return (val*ratio - 2.46*ratio)/(.185*ratio); 00078 case 20: 00079 return (val*ratio - 2.46*ratio)/(.1*ratio); 00080 case 30: 00081 return (val*ratio - 2.46*ratio)/(.066*ratio); 00082 default: 00083 return 999; 00084 } 00085 } 00086 00087 00088 float ACS712::read(){ 00089 return ACS712::translate(sensor * 3.3); 00090 } 00091 00092 ACS712& ACS712::operator=(const ACS712& rhs){ 00093 sensor = rhs.sensor; 00094 ratio = rhs.ratio; 00095 type = rhs.type; 00096 return *this; 00097 }
Generated on Tue Jul 12 2022 12:26:37 by 1.7.2