Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: FATFileSystem mbed
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 ACS712& operator=(const ACS712&); 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 private: 00058 AnalogIn sensor; 00059 float translate(float); 00060 float ratio; 00061 short type; 00062 00063 }; 00064 00065 ACS712::ACS712(PinName _pin, float voltDivRatio, short sensorType) : sensor(_pin){ 00066 ratio = voltDivRatio; 00067 type = sensorType; 00068 } 00069 00070 float ACS712::translate(float val){ 00071 switch(type){ 00072 case 5: 00073 return (val*ratio - 2.46*ratio)/(.185*ratio); 00074 case 20: 00075 return (val*ratio - 2.46*ratio)/(.1*ratio); 00076 case 30: 00077 return (val*ratio - 2.46*ratio)/(.066*ratio); 00078 default: 00079 return 999; 00080 } 00081 } 00082 00083 00084 float ACS712::read(){ 00085 return ACS712::translate(sensor * 3.3); 00086 } 00087 00088 ACS712& ACS712::operator=(const ACS712& rhs){ 00089 sensor = rhs.sensor; 00090 ratio = rhs.ratio; 00091 type = rhs.type; 00092 return *this; 00093 }
Generated on Wed Jul 13 2022 05:59:47 by
