SPI communication with device

Hi,

I have recently encountered issues when trying to communicate with a device using a the recommended library, and I am hoping to develop a very basic level of communication between the arduino and the HX711 module which I am using. I am not very well versed with SPI communication, and therefore I am not sure what direction I should take to address this issue. From my understanding of the data sheet below, https://github.com/sparkfun/HX711-Load-Cell-Amplifier/blob/master/datasheets/hx711_english.pdf

I need to; Setup the SPI pins. Send 25-27 clock pulses down the CLK line Read the data which arrives down the DOUT line

I know this is an over simplification of the process, but I would like to know am I correct, even at this level? Is there a library which deals with type of communication? (please note that the library supplied did not work perfectly [u]FOR ME[/u])

Any sort of feedback would be great, as I am a long way off the level of understanding which I would like.

Below is a link to the component which I am using, https://learn.sparkfun.com/tutorials/load-cell-amplifier-hx711-breakout-hookup-guide

Thanks, MIcheal.

First of all it’s weird a library made for the device does not work. Someone like Sparkfun produce fine working libraries. Maybe not always the most elegant but they do work. So maybe investigate into that first.

Second, did you do any Googling on SPI and Arduino? The info then is very hard to miss ::slight_smile:

But yeah, there are libraries for that. Heck, the Arduino even has hardware for that! The two options, bitShift() which is all in software (not mega slow but works on all pins) or use the hardware SPI module of the Arduino accessible via the SPI library (what’s in the name…)

Hi,

Thanks, I have tried to get the sparkfun library working, and apparently I am the only one see the issues I have, but the output is unpredictable. sometimes I get a reading and sometimes I dont.

There seems to be a huge amount of info out there alright, but I just not sure if I'm learning the appropriate stuff.

This type of communication, which sends just pulses, and not data, with no SS line. Does this fall into a certain category of SPI communication?

Thanks again, Micheal.

As you can see from here, the library uses shiftIn which is serial, but not SPI. It just bit-bangs PD_SCK and reads the data DOUT at the speed of the I/O on your board. What Arduino board or other board are you using? Perhaps its I/O speed is beyond the limitations of the HX711.

Also, you could try making the connections to the HX711 as short as possible.

Providing sample code and connection scheme could be helpful.

Hi, I'm using an arduino UNO. I have approx 400mm between the boards, so maybe there is something there, because, I have followed the instructions perfectly otherwise.

The code I am using is what was supplied on the sparkfun website. But the inconsistencies seem to be with the upload process.

How would I be able to implement these 25 - 27 pulses needed to get a response? and how do I read the data? will it be in 3 Bytes?

Thanks, Micheal.

mmi698: The code I am using is what was supplied on the sparkfun website.

Why not post it?

But the inconsistencies seem to be with the upload process.

And what would that be?

Hi,
This is the code I am using.

/*
 Example using the SparkFun HX711 breakout board with a scale
 By: Nathan Seidle
 SparkFun Electronics
 Date: November 19th, 2014
 License: This code is public domain but you buy me a beer if you use this and we meet someday (Beerware license).
 
 This is the calibration sketch. Use it to determine the calibration_factor that the main example uses. It also
 outputs the zero_factor useful for projects that have a permanent mass on the scale in between power cycles.
 
 Setup your scale and start the sketch WITHOUT a weight on the scale
 Once readings are displayed place the weight on the scale
 Press +/- or a/z to adjust the calibration_factor until the output readings match the known weight
 Use this calibration_factor on the example sketch
 
 This example assumes pounds (lbs). If you prefer kilograms, change the Serial.print(" lbs"); line to kg. The
 calibration factor will be significantly different but it will be linearly related to lbs (1 lbs = 0.453592 kg).
 
 Your calibration factor may be very positive or very negative. It all depends on the setup of your scale system
 and the direction the sensors deflect from zero state
 This example code uses bogde's excellent library: https://github.com/bogde/HX711
 bogde's library is released under a GNU GENERAL PUBLIC LICENSE
 Arduino pin 2 -> HX711 CLK
 3 -> DOUT
 5V -> VCC
 GND -> GND
 
 Most any pin on the Arduino Uno will be compatible with DOUT/CLK.
 
 The HX711 board can be powered from 2.7V to 5V so the Arduino 5V power should be fine.
 
*/

#include "HX711.h"

#define DOUT  3
#define CLK  2

HX711 scale(DOUT, CLK);

float calibration_factor = -7050; //-7050 worked for my 440lb max scale setup

void setup() {
  Serial.begin(9600);
  Serial.println("HX711 calibration sketch");
  Serial.println("Remove all weight from scale");
  Serial.println("After readings begin, place known weight on scale");
  Serial.println("Press + or a to increase calibration factor");
  Serial.println("Press - or z to decrease calibration factor");

  scale.set_scale();
  scale.tare();	//Reset the scale to 0

  long zero_factor = scale.read_average(); //Get a baseline reading
  Serial.print("Zero factor: "); //This can be used to remove the need to tare the scale. Useful in permanent scale projects.
  Serial.println(zero_factor);
}

void loop() {

  scale.set_scale(calibration_factor); //Adjust to this calibration factor

  Serial.print("Reading: ");
  Serial.print(scale.get_units(), 1);
  Serial.print(" lbs"); //Change this to kg and re-adjust the calibration factor if you follow SI units like a sane person
  Serial.print(" calibration_factor: ");
  Serial.print(calibration_factor);
  Serial.println();

  if(Serial.available())
  {
    char temp = Serial.read();
    if(temp == '+' || temp == 'a')
      calibration_factor += 10;
    else if(temp == '-' || temp == 'z')
      calibration_factor -= 10;
  }
}

Rgs, Micheal.

Other SparkFun Examples

Interesting Example that does various tests and includes power down and power up sequence. It uses pins A1 and A0. Note that the print baudrate is 38400.

Can the cables be shortened to < 6 inches (150mm)?

Hi,

Yes, I have tried that code. The problem seems to be across the board with all codes.

One thing that I find suspicious, is a small bit of garbage which is sent to the serial monitor just before the sketch runs. This only seems to happen when there is a bug. When this happens, it is then followed by the text which is outlined in the sketch. (as expected) The next thing I noticed is that when it is time to set the "ZERO FACTOR" the code runs through this piece of code as if it was not there, (it usually takes a few hundred millis to process this), but when the buggy sketch is running, it does not pause here.

I don't understand what is happening behind the scene here. What part of this process could be allowing the process to skip this step? The issue seems to be allowing the process to run faster.

Could this be enough to identify what is going wrong here?

Rgs, Micheal.