A library for interfacing the ACS712 Hall Effect Current Sensor module with a microcontroller
ACS712.cpp@0:c0c26e03d080, 2019-10-10 (annotated)
- Committer:
- JRM1986
- Date:
- Thu Oct 10 11:04:18 2019 +0000
- Revision:
- 0:c0c26e03d080
Pre Publish Commit;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
JRM1986 | 0:c0c26e03d080 | 1 | #include "ACS712.h" |
JRM1986 | 0:c0c26e03d080 | 2 | #include "mbed.h" |
JRM1986 | 0:c0c26e03d080 | 3 | |
JRM1986 | 0:c0c26e03d080 | 4 | // Constructor/Destructor |
JRM1986 | 0:c0c26e03d080 | 5 | ACS712::ACS712(PinName const outpin) |
JRM1986 | 0:c0c26e03d080 | 6 | |
JRM1986 | 0:c0c26e03d080 | 7 | : |
JRM1986 | 0:c0c26e03d080 | 8 | |
JRM1986 | 0:c0c26e03d080 | 9 | _aInPin(new AnalogIn(outpin)) |
JRM1986 | 0:c0c26e03d080 | 10 | |
JRM1986 | 0:c0c26e03d080 | 11 | {} |
JRM1986 | 0:c0c26e03d080 | 12 | |
JRM1986 | 0:c0c26e03d080 | 13 | ACS712::~ACS712() |
JRM1986 | 0:c0c26e03d080 | 14 | { |
JRM1986 | 0:c0c26e03d080 | 15 | delete _aInPin; |
JRM1986 | 0:c0c26e03d080 | 16 | } |
JRM1986 | 0:c0c26e03d080 | 17 | |
JRM1986 | 0:c0c26e03d080 | 18 | // ----- Public Methods ----- |
JRM1986 | 0:c0c26e03d080 | 19 | |
JRM1986 | 0:c0c26e03d080 | 20 | void ACS712::init() |
JRM1986 | 0:c0c26e03d080 | 21 | { |
JRM1986 | 0:c0c26e03d080 | 22 | |
JRM1986 | 0:c0c26e03d080 | 23 | } |
JRM1986 | 0:c0c26e03d080 | 24 | double ACS712::read_current_sensor() |
JRM1986 | 0:c0c26e03d080 | 25 | { |
JRM1986 | 0:c0c26e03d080 | 26 | double val; |
JRM1986 | 0:c0c26e03d080 | 27 | val = _aInPin->read(); |
JRM1986 | 0:c0c26e03d080 | 28 | val *= val; |
JRM1986 | 0:c0c26e03d080 | 29 | val = sqrt(val); |
JRM1986 | 0:c0c26e03d080 | 30 | return val; |
JRM1986 | 0:c0c26e03d080 | 31 | } |
JRM1986 | 0:c0c26e03d080 | 32 | |
JRM1986 | 0:c0c26e03d080 | 33 | int ACS712::read_ain_uint_16() |
JRM1986 | 0:c0c26e03d080 | 34 | { |
JRM1986 | 0:c0c26e03d080 | 35 | int val = 0; |
JRM1986 | 0:c0c26e03d080 | 36 | |
JRM1986 | 0:c0c26e03d080 | 37 | val = _aInPin->read_u16(); |
JRM1986 | 0:c0c26e03d080 | 38 | |
JRM1986 | 0:c0c26e03d080 | 39 | return val; |
JRM1986 | 0:c0c26e03d080 | 40 | } |
JRM1986 | 0:c0c26e03d080 | 41 | |
JRM1986 | 0:c0c26e03d080 | 42 | void ACS712::convert_to_amps(float acs_offset, float gain, int type) |
JRM1986 | 0:c0c26e03d080 | 43 | { |
JRM1986 | 0:c0c26e03d080 | 44 | |
JRM1986 | 0:c0c26e03d080 | 45 | double _current_in = read_current_sensor()*3.3; |
JRM1986 | 0:c0c26e03d080 | 46 | |
JRM1986 | 0:c0c26e03d080 | 47 | switch(type) { |
JRM1986 | 0:c0c26e03d080 | 48 | |
JRM1986 | 0:c0c26e03d080 | 49 | case 5: |
JRM1986 | 0:c0c26e03d080 | 50 | _current = (acs_offset*gain - _current_in*gain)/.185*gain; |
JRM1986 | 0:c0c26e03d080 | 51 | break; |
JRM1986 | 0:c0c26e03d080 | 52 | case 20: |
JRM1986 | 0:c0c26e03d080 | 53 | _current = (acs_offset*gain - _current_in*gain)/.100*gain; |
JRM1986 | 0:c0c26e03d080 | 54 | break; |
JRM1986 | 0:c0c26e03d080 | 55 | case 30: |
JRM1986 | 0:c0c26e03d080 | 56 | _current = (_current_in*gain - acs_offset*gain)/.066*gain; |
JRM1986 | 0:c0c26e03d080 | 57 | break; |
JRM1986 | 0:c0c26e03d080 | 58 | default: |
JRM1986 | 0:c0c26e03d080 | 59 | _current = 999.9; |
JRM1986 | 0:c0c26e03d080 | 60 | break; |
JRM1986 | 0:c0c26e03d080 | 61 | |
JRM1986 | 0:c0c26e03d080 | 62 | } |
JRM1986 | 0:c0c26e03d080 | 63 | |
JRM1986 | 0:c0c26e03d080 | 64 | } |
JRM1986 | 0:c0c26e03d080 | 65 | |
JRM1986 | 0:c0c26e03d080 | 66 | |
JRM1986 | 0:c0c26e03d080 | 67 | bool ACS712::over_current_detection(double operating_current) |
JRM1986 | 0:c0c26e03d080 | 68 | { |
JRM1986 | 0:c0c26e03d080 | 69 | |
JRM1986 | 0:c0c26e03d080 | 70 | bool current_spike_flag = false; |
JRM1986 | 0:c0c26e03d080 | 71 | |
JRM1986 | 0:c0c26e03d080 | 72 | if(_current > (1.1*operating_current)) { |
JRM1986 | 0:c0c26e03d080 | 73 | |
JRM1986 | 0:c0c26e03d080 | 74 | current_spike_flag = true; |
JRM1986 | 0:c0c26e03d080 | 75 | |
JRM1986 | 0:c0c26e03d080 | 76 | } |
JRM1986 | 0:c0c26e03d080 | 77 | else { |
JRM1986 | 0:c0c26e03d080 | 78 | |
JRM1986 | 0:c0c26e03d080 | 79 | current_spike_flag = false; |
JRM1986 | 0:c0c26e03d080 | 80 | |
JRM1986 | 0:c0c26e03d080 | 81 | } |
JRM1986 | 0:c0c26e03d080 | 82 | |
JRM1986 | 0:c0c26e03d080 | 83 | return current_spike_flag; |
JRM1986 | 0:c0c26e03d080 | 84 | |
JRM1986 | 0:c0c26e03d080 | 85 | } |
JRM1986 | 0:c0c26e03d080 | 86 | |
JRM1986 | 0:c0c26e03d080 | 87 | float ACS712::get_current_amps() |
JRM1986 | 0:c0c26e03d080 | 88 | { |
JRM1986 | 0:c0c26e03d080 | 89 | |
JRM1986 | 0:c0c26e03d080 | 90 | return _current; |
JRM1986 | 0:c0c26e03d080 | 91 | |
JRM1986 | 0:c0c26e03d080 | 92 | } |