Jump to content

[Energia Library] HX711 library for use in Energia

Recommended Posts

So I found that I was missing something very basic, #include "Energia.h". This did fix a lot of problem but now, these are the error codes that I am getting when I compile the code I am working on in Energia.



Energia: 1.6.10E18 (Mac OS X), Board: "MSP-EXP430F5529LP"

/Users/Jon/Documents/Energia/libraries/HX711/HX711.cpp: In member function 'long int HX711::read()':
/Users/Jon/Documents/Energia/libraries/HX711/HX711.cpp:53:9: error: 'yield' was not declared in this scope
/Users/Jon/Documents/Energia/libraries/HX711/HX711.cpp: In member function 'long int HX711::read_average(byte)':
/Users/Jon/Documents/Energia/libraries/HX711/HX711.cpp:91:9: error: 'yield' was not declared in this scope
exit status 1
Error compiling for board MSP-EXP430F5529LP.
Edited .h file from the Github repository
    Header file - Library for HX711

#ifndef HX711_h
#define HX711_h

#include "Energia.h"

class HX711
		byte PD_SCK;	// Power Down and Serial Clock Input Pin
		byte DOUT;		// Serial Data Output Pin
		byte GAIN;		// amplification factor
		long OFFSET;	// used for tare weight
		float SCALE;	// used to return weight in grams, kg, ounces, whatever

		// define clock and data pin, channel, and gain factor
		// channel selection is made by passing the appropriate gain: 128 or 64 for channel A, 32 for channel B
		// gain: 128 or 64 for channel A; channel B works with 32 gain factor only
		HX711(byte dout, byte pd_sck, byte gain = 128);


		virtual ~HX711();

		// Allows to set the pins and gain later than in the constructor
		void begin(byte dout, byte pd_sck, byte gain = 128);

		// check if HX711 is ready
		// from the datasheet: When output data is not ready for retrieval, digital output pin DOUT is high. Serial clock
		// input PD_SCK should be low. When DOUT goes to low, it indicates data is ready for retrieval.
		bool is_ready();

		// set the gain factor; takes effect only after a call to read()
		// channel A can be set for a 128 or 64 gain; channel B has a fixed 32 gain
		// depending on the parameter, the channel is also set to either A or B
		void set_gain(byte gain = 128);

		// waits for the chip to be ready and returns a reading
		long read();

		// returns an average reading; times = how many times to read
		long read_average(byte times = 10);

		// returns (read_average() - OFFSET), that is the current value without the tare weight; times = how many readings to do
		double get_value(byte times = 1);

		// returns get_value() divided by SCALE, that is the raw value divided by a value obtained via calibration
		// times = how many readings to do
		float get_units(byte times = 1);

		// set the OFFSET value for tare weight; times = how many times to read the tare value
		void tare(byte times = 10);

		// set the SCALE value; this value is used to convert the raw data to "human readable" data (measure units)
		void set_scale(float scale = 1.f);

		// get the current SCALE
		float get_scale();

		// set OFFSET, the value that's subtracted from the actual reading (tare weight)
		void set_offset(long offset = 0);

		// get the current OFFSET
		long get_offset();

		// puts the chip into power down mode
		void power_down();

		// wakes up the chip after power down mode
		void power_up();

#endif /* HX711_h */

Edited .cpp file from Github repository

 Source file - Library for HX711

#include "Energia.h"
#include <HX711.h>

HX711::HX711(byte dout, byte pd_sck, byte gain) {
	begin(dout, pd_sck, gain);

HX711::HX711() {

HX711::~HX711() {

void HX711::begin(byte dout, byte pd_sck, byte gain) {
	PD_SCK = pd_sck;
	DOUT = dout;

	pinMode(PD_SCK, OUTPUT);
	pinMode(DOUT, INPUT);


bool HX711::is_ready() {
	return digitalRead(DOUT) == LOW;

void HX711::set_gain(byte gain) {
	switch (gain) {
		case 128:		// channel A, gain factor 128
			GAIN = 1;
		case 64:		// channel A, gain factor 64
			GAIN = 3;
		case 32:		// channel B, gain factor 32
			GAIN = 2;

	digitalWrite(PD_SCK, LOW);

long HX711::read() {
	// wait for the chip to become ready
	while (!is_ready()) {
		// Will do nothing on Arduino but prevent resets of ESP8266 (Watchdog Issue)

	unsigned long value = 0;
	uint8_t data[3] = { 0 };
	uint8_t filler = 0x00;

	// pulse the clock pin 24 times to read the data
	data[2] = shiftIn(DOUT, PD_SCK, MSBFIRST);
	data[1] = shiftIn(DOUT, PD_SCK, MSBFIRST);
	data[0] = shiftIn(DOUT, PD_SCK, MSBFIRST);

	// set the channel and the gain factor for the next reading using the clock pin
	for (unsigned int i = 0; i < GAIN; i++) {
		digitalWrite(PD_SCK, HIGH);
		digitalWrite(PD_SCK, LOW);

	// Replicate the most significant bit to pad out a 32-bit signed integer
	if (data[2] & 0x80) {
		filler = 0xFF;
	} else {
		filler = 0x00;

	// Construct a 32-bit signed integer
	value = ( static_cast<unsigned long>(filler) << 24
			| static_cast<unsigned long>(data[2]) << 16
			| static_cast<unsigned long>(data[1]) << 8
			| static_cast<unsigned long>(data[0]) );

	return static_cast<long>(value);

long HX711::read_average(byte times) {
	long sum = 0;
	for (byte i = 0; i < times; i++) {
		sum += read();
	return sum / times;

double HX711::get_value(byte times) {
    OFFSET = 0;
	return read_average(times) - OFFSET;

float HX711::get_units(byte times) {
    SCALE = 0;
	return get_value(times) / SCALE;

void HX711::tare(byte times) {
	double sum = read_average(times);

void HX711::set_scale(float scale) {
	SCALE = scale;

float HX711::get_scale() {
	return SCALE;

void HX711::set_offset(long offset) {
	OFFSET = offset;

long HX711::get_offset() {
	return OFFSET;

void HX711::power_down() {
	digitalWrite(PD_SCK, LOW);
	digitalWrite(PD_SCK, HIGH);

void HX711::power_up() {
	digitalWrite(PD_SCK, LOW);

Link to post
Share on other sites

Per the error message, 'yield' was not declared in this scope


If you look in the code....

long HX711::read() {
	// wait for the chip to become ready
	while (!is_ready()) {
		// Will do nothing on Arduino but prevent resets of ESP8266 (Watchdog Issue)

So it appears that yield() is only needed for the ESP8266.  You can try going through and commenting out yield() wherever it occurs.

Link to post
Share on other sites
  • bluehash changed the title to [Energia Library] HX711 library for use in Energia

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Create New...