Library for communication with the HX711 24-Bit Analog-to-Digital converter.

Dependents:   HMC1501_Hello

Library for communication with the HX711 24-Bit Analog-to-Digital converter.

HX711

400

HX711 is a precision 24-bit analog-to-digital converter (ADC) designed for weigh scales and industrial control applications to interface directly with a bridge sensor. The input multiplexer selects either Channel A or B differential input to the low-noise programmable gain amplifier (PGA). Channel A can be programmed with a gain of 128 or 64, corresponding to a full-scale differential input voltage of ±20mV or ±40mV respectively, when a 5V supply is connected to AVDD analog power supply pin. Channel B has a fixed gain of 32. On-chip power supply regulator eliminates the need for an external supply regulator to provide analog power for the ADC and the sensor. Clock input is flexible. It can be from an external clock source, a crystal, or the on-chip oscillator that does not require any external component. On-chip power-on-reset circuitry simplifies digital interface initialization.

https://os.mbed.com/media/uploads/hudakz/hx711_sch.png

Example program:

Import programHMC1501_Hello

Example program for the HMC1501 magnetic 90° angle sensor connected to a HX711 high precision 24-bit programmable analog-to-digital converter (ADC) .

Committer:
hudakz
Date:
Sat Sep 19 17:54:17 2020 +0000
Revision:
0:72a9d7812a33
Library for communication with the HX711 24-Bit Analog-to-Digital converter.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
hudakz 0:72a9d7812a33 1 /*
hudakz 0:72a9d7812a33 2 * Copyright (c) 2020 Zoltan Hudak <hudakz@outlook.com>
hudakz 0:72a9d7812a33 3 * All rights reserved.
hudakz 0:72a9d7812a33 4 *
hudakz 0:72a9d7812a33 5 * This program is free software: you can redistribute it and/or modify
hudakz 0:72a9d7812a33 6 * it under the terms of the GNU General Public License as published by
hudakz 0:72a9d7812a33 7 * the Free Software Foundation, either version 3 of the License, or
hudakz 0:72a9d7812a33 8 * (at your option) any later version.
hudakz 0:72a9d7812a33 9 *
hudakz 0:72a9d7812a33 10 * This program is distributed in the hope that it will be useful,
hudakz 0:72a9d7812a33 11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
hudakz 0:72a9d7812a33 12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
hudakz 0:72a9d7812a33 13 * GNU General Public License for more details.
hudakz 0:72a9d7812a33 14 *
hudakz 0:72a9d7812a33 15 * You should have received a copy of the GNU General Public License
hudakz 0:72a9d7812a33 16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
hudakz 0:72a9d7812a33 17 */
hudakz 0:72a9d7812a33 18
hudakz 0:72a9d7812a33 19 #ifndef _HX711_H_
hudakz 0:72a9d7812a33 20 #define _HX711_H_
hudakz 0:72a9d7812a33 21 #include "mbed.h"
hudakz 0:72a9d7812a33 22
hudakz 0:72a9d7812a33 23 /**
hudakz 0:72a9d7812a33 24 * Class for communication with the HX711 24-Bit Analog-to-Digital converter (ADC)
hudakz 0:72a9d7812a33 25 * (Developed for Weigh Scales by AVIA Semiconductor.)
hudakz 0:72a9d7812a33 26 * This library is based on the library at https://github.com/bogde/HX711
hudakz 0:72a9d7812a33 27 */
hudakz 0:72a9d7812a33 28 class HX711
hudakz 0:72a9d7812a33 29 {
hudakz 0:72a9d7812a33 30 public:
hudakz 0:72a9d7812a33 31 /**
hudakz 0:72a9d7812a33 32 * Creates an HX711 object
hudakz 0:72a9d7812a33 33 * @param avdd Analog power supply voltage [V]
hudakz 0:72a9d7812a33 34 * @param sck PinName of the clock pin (digital output)
hudakz 0:72a9d7812a33 35 * @param dout PinName of the data pin (digital input)
hudakz 0:72a9d7812a33 36 * @param drift Chip's input offset drift [V]
hudakz 0:72a9d7812a33 37 * @param gain Channel selection is made by passing the associated gain:
hudakz 0:72a9d7812a33 38 * 128 or 64 for channel A, 32 for channel B
hudakz 0:72a9d7812a33 39 */
hudakz 0:72a9d7812a33 40 HX711(float avdd, PinName sck, PinName dout, uint8_t gain, float drift = 0) :
hudakz 0:72a9d7812a33 41 _avdd(avdd),
hudakz 0:72a9d7812a33 42 _sck(sck),
hudakz 0:72a9d7812a33 43 _dout(dout, PullUp),
hudakz 0:72a9d7812a33 44 _drift(drift)
hudakz 0:72a9d7812a33 45 {
hudakz 0:72a9d7812a33 46 setGain(gain);
hudakz 0:72a9d7812a33 47 }
hudakz 0:72a9d7812a33 48
hudakz 0:72a9d7812a33 49 /**
hudakz 0:72a9d7812a33 50 * Check if the sensor is ready.
hudakz 0:72a9d7812a33 51 * From the datasheet: When output data is not ready for retrieval,
hudakz 0:72a9d7812a33 52 * digital output pin _dout is high. Serial clock input _sck should be low.
hudakz 0:72a9d7812a33 53 * When _dout goes low, it indicates data is ready for retrieval.
hudakz 0:72a9d7812a33 54 * @return true if _dout.read() == 0
hudakz 0:72a9d7812a33 55 */
hudakz 0:72a9d7812a33 56 bool isReady() { return _dout.read() == 0; }
hudakz 0:72a9d7812a33 57
hudakz 0:72a9d7812a33 58 /**
hudakz 0:72a9d7812a33 59 * Returns a raw int reading
hudakz 0:72a9d7812a33 60 * @return int sensor output value
hudakz 0:72a9d7812a33 61 */
hudakz 0:72a9d7812a33 62 uint32_t readInt();
hudakz 0:72a9d7812a33 63
hudakz 0:72a9d7812a33 64 /**
hudakz 0:72a9d7812a33 65 * Obtains input voltage in mV biased by chip's input offset drift
hudakz 0:72a9d7812a33 66 * @return float
hudakz 0:72a9d7812a33 67 */
hudakz 0:72a9d7812a33 68 float read();
hudakz 0:72a9d7812a33 69
hudakz 0:72a9d7812a33 70 /**
hudakz 0:72a9d7812a33 71 * Puts the chip into power down mode
hudakz 0:72a9d7812a33 72 */
hudakz 0:72a9d7812a33 73 void powerDown() { _sck.write(0); _sck.write(1); }
hudakz 0:72a9d7812a33 74
hudakz 0:72a9d7812a33 75 /**
hudakz 0:72a9d7812a33 76 * Wakes up the chip after power down mode
hudakz 0:72a9d7812a33 77 */
hudakz 0:72a9d7812a33 78 void powerUp() { _sck.write(0); }
hudakz 0:72a9d7812a33 79
hudakz 0:72a9d7812a33 80 /**
hudakz 0:72a9d7812a33 81 * Set the gain factor; takes effect only after a call to read()
hudakz 0:72a9d7812a33 82 * channel A can be set for a 128 or 64 gain; channel B has a fixed 32 gain
hudakz 0:72a9d7812a33 83 * depending on the parameter, the channel is also set to either A or B
hudakz 0:72a9d7812a33 84 * Ensures that gain_ = 128, 64 or 32
hudakz 0:72a9d7812a33 85 * @param gain 128, 64 or 32
hudakz 0:72a9d7812a33 86 */
hudakz 0:72a9d7812a33 87 void setGain(uint8_t gain = 128);
hudakz 0:72a9d7812a33 88
hudakz 0:72a9d7812a33 89 /**
hudakz 0:72a9d7812a33 90 * Obtain current gain
hudakz 0:72a9d7812a33 91 * @return gain_
hudakz 0:72a9d7812a33 92 */
hudakz 0:72a9d7812a33 93 uint8_t getGain() { return _mode; }
hudakz 0:72a9d7812a33 94
hudakz 0:72a9d7812a33 95 /**
hudakz 0:72a9d7812a33 96 * Get sensor's analog power supply voltage
hudakz 0:72a9d7812a33 97 * @return power supply voltage
hudakz 0:72a9d7812a33 98 */
hudakz 0:72a9d7812a33 99 float getAvdd() { return _avdd; }
hudakz 0:72a9d7812a33 100
hudakz 0:72a9d7812a33 101 /**
hudakz 0:72a9d7812a33 102 * Sets the chip's input offset drift
hudakz 0:72a9d7812a33 103 * @param drift The input offset drift [mV]
hudakz 0:72a9d7812a33 104 */
hudakz 0:72a9d7812a33 105 void setDrift(float drift = 0) { _drift = drift; }
hudakz 0:72a9d7812a33 106
hudakz 0:72a9d7812a33 107 /**
hudakz 0:72a9d7812a33 108 * Gets chip's input offset drift
hudakz 0:72a9d7812a33 109 * @return Chip's input offset drift [mV]
hudakz 0:72a9d7812a33 110 */
hudakz 0:72a9d7812a33 111 int getDrift() { return(_drift); }
hudakz 0:72a9d7812a33 112
hudakz 0:72a9d7812a33 113 private:
hudakz 0:72a9d7812a33 114 float _avdd; // analog power supply voltage [V]
hudakz 0:72a9d7812a33 115 DigitalOut _sck; // HX711 clock input line
hudakz 0:72a9d7812a33 116 DigitalIn _dout; // HX711 data output line
hudakz 0:72a9d7812a33 117 uint8_t _gain; // amplification factor of HX711
hudakz 0:72a9d7812a33 118 uint8_t _mode; // operating mode of HX711
hudakz 0:72a9d7812a33 119 float _drift; // chip's input offset drift [mV]
hudakz 0:72a9d7812a33 120
hudakz 0:72a9d7812a33 121 /**
hudakz 0:72a9d7812a33 122 * @brief Converts a 32-bit signed fixed point integer to floating point value
hudakz 0:72a9d7812a33 123 * @note The 32-bit unsigned integer argument represnts actually a 24-bit signed fixed point value:
hudakz 0:72a9d7812a33 124 * two’s complement and the LSB of the 24-bit binary number represents 1/8,388,608th of a unit).
hudakz 0:72a9d7812a33 125 * @param 32-bit unsigned integer
hudakz 0:72a9d7812a33 126 * @retval Floating point value ranging from -1 to +1
hudakz 0:72a9d7812a33 127 */
hudakz 0:72a9d7812a33 128 float _toFloat(uint32_t dword);
hudakz 0:72a9d7812a33 129
hudakz 0:72a9d7812a33 130 /**
hudakz 0:72a9d7812a33 131 * Converts floating point value to input voltage
hudakz 0:72a9d7812a33 132 * @param val Floating point value ranging from -1.0 to +1.0
hudakz 0:72a9d7812a33 133 * @return Voltage at input [V]
hudakz 0:72a9d7812a33 134 */
hudakz 0:72a9d7812a33 135 float toVolt(float val);
hudakz 0:72a9d7812a33 136 };
hudakz 0:72a9d7812a33 137 #endif