A library for interfacing the ACS712 Hall Effect Current Sensor module with a microcontroller
Revision 0:c0c26e03d080, committed 2019-10-10
- Comitter:
- JRM1986
- Date:
- Thu Oct 10 11:04:18 2019 +0000
- Commit message:
- Pre Publish Commit;
Changed in this revision
| ACS712.cpp | Show annotated file Show diff for this revision Revisions of this file |
| ACS712.h | Show annotated file Show diff for this revision Revisions of this file |
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/ACS712.cpp Thu Oct 10 11:04:18 2019 +0000
@@ -0,0 +1,92 @@
+#include "ACS712.h"
+#include "mbed.h"
+
+// Constructor/Destructor
+ACS712::ACS712(PinName const outpin)
+
+ :
+
+ _aInPin(new AnalogIn(outpin))
+
+{}
+
+ACS712::~ACS712()
+{
+ delete _aInPin;
+}
+
+// ----- Public Methods -----
+
+void ACS712::init()
+{
+
+}
+double ACS712::read_current_sensor()
+{
+ double val;
+ val = _aInPin->read();
+ val *= val;
+ val = sqrt(val);
+ return val;
+}
+
+int ACS712::read_ain_uint_16()
+{
+ int val = 0;
+
+ val = _aInPin->read_u16();
+
+ return val;
+}
+
+void ACS712::convert_to_amps(float acs_offset, float gain, int type)
+{
+
+ double _current_in = read_current_sensor()*3.3;
+
+ switch(type) {
+
+ case 5:
+ _current = (acs_offset*gain - _current_in*gain)/.185*gain;
+ break;
+ case 20:
+ _current = (acs_offset*gain - _current_in*gain)/.100*gain;
+ break;
+ case 30:
+ _current = (_current_in*gain - acs_offset*gain)/.066*gain;
+ break;
+ default:
+ _current = 999.9;
+ break;
+
+ }
+
+}
+
+
+bool ACS712::over_current_detection(double operating_current)
+{
+
+ bool current_spike_flag = false;
+
+ if(_current > (1.1*operating_current)) {
+
+ current_spike_flag = true;
+
+ }
+ else {
+
+ current_spike_flag = false;
+
+ }
+
+ return current_spike_flag;
+
+}
+
+float ACS712::get_current_amps()
+{
+
+ return _current;
+
+}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/ACS712.h Thu Oct 10 11:04:18 2019 +0000
@@ -0,0 +1,101 @@
+#ifndef ACS712_H
+#define ACS712_H
+
+#define WAIT 5.0
+#include "mbed.h"
+
+/**
+ @file ACS712.h
+* @brief Library for interfacing ACS712 Hall Effect Current sensor
+*
+* @author Josh Marshall
+*
+* @date July 2019
+*/
+
+
+namespace mbed
+{
+ class AnalogIn;
+}
+
+class ACS712
+{
+
+private:
+
+ mbed::AnalogIn *_aInPin;
+
+public:
+
+ /** Constructor */
+ ACS712(PinName const outpin);
+
+ /** Destructor */
+ ~ACS712();
+
+/**
+* Initialise all peripherals and configure interrupts
+* @author Josh Marshall
+* @date July 2019
+*/
+void init();
+
+
+/**
+* Reads current sensor
+* @returns current sensor value in range 0.0 to 1.0
+* @author Josh Marshall
+* @date July 2019
+*/
+double read_current_sensor();
+
+
+/**
+* Read current sensor
+* @returns current sensor value in range 0 to 65535 (2^16-1)
+* K64F High‐speed 16‐bit ADC with configurable resolution
+* LPC1768 12-bit Analog-to-Digital Converter (ADC) with input multiplexing among
+* eight pins, conversion rates up to 200 kHz, and multiple result registers.
+* @author Josh Marshall
+* @date July 2019
+*/
+
+
+int read_ain_uint_16();
+
+/**
+* Read current sensor and convert to amps
+* @param acs_ffset the value that the current sensor outputs at 0 amps
+* @param gain from potential divider to map 5v out put of sensor onto 3.3v input
+* @param sensor type 5,20, or 30 amp
+* @author Josh Marshall
+* @date July 2019
+*/
+void convert_to_amps(float acs_offset, float gain, int type);
+
+/**
+* If current spike (>1.1*operating current) from motor is detected it returns true flag
+* @param value of current from ACS712 Hall Effect Current Sensor
+* @prarm typical operating current
+* @author Josh Marshall
+* @date July 2019
+*/
+
+bool over_current_detection(double operating_current);
+
+/**
+* Returns the value frrom the current sensor
+* @author Josh Marshall
+* @date July 2019
+*/
+
+float get_current_amps();
+
+private:
+
+ double _current;
+
+};
+
+#endif
\ No newline at end of file