Integrating Arduino , hx711 and 3-digit, 7-segment display

welcome everybody

i need help

i have:
Arduino Mega 2560
hx711
3-digit, 7-segment display

Now I have the weight code, which is as follows

#include "HX711.h"

#define DOUT  32
#define CLK  30

HX711 scale;

float calibration_factor = -100000; //Change this for calibration your load cell

void setup() {
  Serial.begin(9600);

  
  scale.begin(DOUT, CLK);
  scale.set_scale();
  scale.tare(); //Reset the scale to 0
  

}

void loop() {

  scale.set_scale(calibration_factor); //Adjust to this calibration factor
  Serial.print(abs(scale.get_units())*100,0);
  Serial.println();

} 

The weight is displayed on the serial screen

I have the code for the screen, which is the following

int time = 5;


int pinA = 2;
int pinB = 3;
int pinC = 4;
int pinD = 5;
int pinE = 6;
int pinF = 7;
int pinG = 8;


int DIG1 = 10;
int DIG2 = 11;
int DIG3 = 12;



void choose_digit(char num) {
  switch(num) {
    default:
     // 0 default value 
digitalWrite(pinA, HIGH);
digitalWrite(pinB, HIGH);
digitalWrite(pinC, HIGH);
digitalWrite(pinD, HIGH);
digitalWrite(pinE, HIGH);
digitalWrite(pinF, HIGH);
digitalWrite(pinG, LOW);
break;
case 1:
digitalWrite(pinA, LOW);
digitalWrite(pinB, HIGH);
digitalWrite(pinC, HIGH);
digitalWrite(pinD, LOW);
digitalWrite(pinE, LOW);
digitalWrite(pinF, LOW);
digitalWrite(pinG, LOW);
break;
case 2:
digitalWrite(pinA, HIGH);
digitalWrite(pinB, HIGH);
digitalWrite(pinC, LOW);
digitalWrite(pinD, HIGH);
digitalWrite(pinE, HIGH);
digitalWrite(pinF, LOW);
digitalWrite(pinG, HIGH);
break;
case 3:
digitalWrite(pinA, HIGH);
digitalWrite(pinB, HIGH);
digitalWrite(pinC, HIGH);
digitalWrite(pinD, HIGH);
digitalWrite(pinE, LOW);
digitalWrite(pinF, LOW);
digitalWrite(pinG, HIGH);
break;
case 4:
digitalWrite(pinA, LOW);
digitalWrite(pinB, HIGH);
digitalWrite(pinC, HIGH);
digitalWrite(pinD, LOW);
digitalWrite(pinE, LOW);
digitalWrite(pinF, HIGH);
digitalWrite(pinG, HIGH);
break;
case 5:
digitalWrite(pinA, HIGH);
digitalWrite(pinB, LOW);
digitalWrite(pinC, HIGH);
digitalWrite(pinD, HIGH);
digitalWrite(pinE, LOW);
digitalWrite(pinF, HIGH);
digitalWrite(pinG, HIGH);
break;
case 6:
digitalWrite(pinA, HIGH);
digitalWrite(pinB, LOW);
digitalWrite(pinC, HIGH);
digitalWrite(pinD, HIGH);
digitalWrite(pinE, HIGH);
digitalWrite(pinF, HIGH);
digitalWrite(pinG, HIGH);
break;
case 7:
digitalWrite(pinA, HIGH);
digitalWrite(pinB, HIGH);
digitalWrite(pinC, HIGH);
digitalWrite(pinD, LOW);
digitalWrite(pinE, LOW);
digitalWrite(pinF, LOW);
digitalWrite(pinG, LOW);
break;
case 8:
digitalWrite(pinA, HIGH);
digitalWrite(pinB, HIGH);
digitalWrite(pinC, HIGH);
digitalWrite(pinD, HIGH);
digitalWrite(pinE, HIGH);
digitalWrite(pinF, HIGH);
digitalWrite(pinG, HIGH);
break;
case 9:
digitalWrite(pinA, HIGH);
digitalWrite(pinB, HIGH);
digitalWrite(pinC, HIGH);
digitalWrite(pinD, HIGH);
digitalWrite(pinE, LOW);
digitalWrite(pinF, HIGH);
digitalWrite(pinG, HIGH);
break;

}
}

void pick_digit(int digit){
  digitalWrite(DIG1, HIGH);
  digitalWrite(DIG2, HIGH);
  digitalWrite(DIG3, HIGH);


switch(digit){
  case 1: digitalWrite(DIG1, LOW);break;
  case 2: digitalWrite(DIG2, LOW);break;
  default: digitalWrite(DIG3, LOW);break;

}
}
void seven_segment(int number){
  
  unsigned char hundreds = int (number/100);
  unsigned char tens     = int ((number/10)%10);
  unsigned char ones     = int (number%10);
 
//123
choose_digit(hundreds);
pick_digit(1);
delay(time);

choose_digit(tens);
pick_digit(2);
delay(time);

choose_digit(ones);
pick_digit(3);
delay(time);

}
///////////////////
 
///////////////////

void setup() {
  
  pinMode(DIG1, OUTPUT);
  pinMode(DIG2, OUTPUT);
  pinMode(DIG3, OUTPUT);
  pinMode(pinA, OUTPUT);
  pinMode(pinB, OUTPUT);
  pinMode(pinC, OUTPUT);
  pinMode(pinD, OUTPUT);
  pinMode(pinE, OUTPUT);
  pinMode(pinF, OUTPUT);
  pinMode(pinG, OUTPUT);

  Serial.begin(9600);
//////////////////
 
//////////////////
}


void loop() {
int sensor_value = A0;
Serial.println(sensor_value);
seven_segment(sensor_value);



delay(1);

//////////////////
 
//////////////////
}

The number appears on the screen according to the value of A0

Please help with the following code:
I want to display the weight value on a 3-digit 7-segment display
Because my max weight is 280kg

Can you help me?
thank you all

Combine the global variables.
Combine both setup() functions.
Combine both loop() functions.

Since the 7-segment display needs to be updated as often as possible, I put reading the HX711 in a millis timer.

I smashed everything together and got something that does something :face_with_raised_eyebrow:
It is still full of bugs, but I didn't have to time to make it a good sketch.

// Forum: https://forum.arduino.cc/t/integrating-arduino-hx711-and-3-digit-7-segment-display/1167152
// This Wokwi project: https://wokwi.com/projects/375502753549598721
// Warning: full of bugs

#include <HX711.h>

// The used pins
const int DOUT = 32;
const int CLK = 30;

const int pinA = 2;
const int pinB = 3;
const int pinC = 4;
const int pinD = 5;
const int pinE = 6;
const int pinF = 7;
const int pinG = 8;
const int DIG1 = 10;
const int DIG2 = 11;
const int DIG3 = 12;

int time = 5;

HX711 scale;

float calibration_factor = 0; //Change this for calibration your load cell

float weight = 0.0;

unsigned long previousMillis;
const unsigned long interval = 500;

void setup() 
{
  Serial.begin(9600);

  pinMode(DIG1, OUTPUT);
  pinMode(DIG2, OUTPUT);
  pinMode(DIG3, OUTPUT);
  pinMode(pinA, OUTPUT);
  pinMode(pinB, OUTPUT);
  pinMode(pinC, OUTPUT);
  pinMode(pinD, OUTPUT);
  pinMode(pinE, OUTPUT);
  pinMode(pinF, OUTPUT);
  pinMode(pinG, OUTPUT);

  scale.begin(DOUT, CLK);
//  scale.set_scale();
//  scale.tare();             //Reset the scale to 0
}

void loop() 
{
  unsigned long currentMillis = millis();

  if( currentMillis - previousMillis >= interval)
  {
    previousMillis = currentMillis;

    // scale.set_scale(calibration_factor); //Adjust to this calibration factor
    weight = scale.get_units();
    Serial.println(weight, 1);
  }

  seven_segment((int)weight);
}

void choose_digit(char num) {
  switch (num) {
    default:
      // 0 default value
      digitalWrite(pinA, HIGH);
      digitalWrite(pinB, HIGH);
      digitalWrite(pinC, HIGH);
      digitalWrite(pinD, HIGH);
      digitalWrite(pinE, HIGH);
      digitalWrite(pinF, HIGH);
      digitalWrite(pinG, LOW);
      break;
    case 1:
      digitalWrite(pinA, LOW);
      digitalWrite(pinB, HIGH);
      digitalWrite(pinC, HIGH);
      digitalWrite(pinD, LOW);
      digitalWrite(pinE, LOW);
      digitalWrite(pinF, LOW);
      digitalWrite(pinG, LOW);
      break;
    case 2:
      digitalWrite(pinA, HIGH);
      digitalWrite(pinB, HIGH);
      digitalWrite(pinC, LOW);
      digitalWrite(pinD, HIGH);
      digitalWrite(pinE, HIGH);
      digitalWrite(pinF, LOW);
      digitalWrite(pinG, HIGH);
      break;
    case 3:
      digitalWrite(pinA, HIGH);
      digitalWrite(pinB, HIGH);
      digitalWrite(pinC, HIGH);
      digitalWrite(pinD, HIGH);
      digitalWrite(pinE, LOW);
      digitalWrite(pinF, LOW);
      digitalWrite(pinG, HIGH);
      break;
    case 4:
      digitalWrite(pinA, LOW);
      digitalWrite(pinB, HIGH);
      digitalWrite(pinC, HIGH);
      digitalWrite(pinD, LOW);
      digitalWrite(pinE, LOW);
      digitalWrite(pinF, HIGH);
      digitalWrite(pinG, HIGH);
      break;
    case 5:
      digitalWrite(pinA, HIGH);
      digitalWrite(pinB, LOW);
      digitalWrite(pinC, HIGH);
      digitalWrite(pinD, HIGH);
      digitalWrite(pinE, LOW);
      digitalWrite(pinF, HIGH);
      digitalWrite(pinG, HIGH);
      break;
    case 6:
      digitalWrite(pinA, HIGH);
      digitalWrite(pinB, LOW);
      digitalWrite(pinC, HIGH);
      digitalWrite(pinD, HIGH);
      digitalWrite(pinE, HIGH);
      digitalWrite(pinF, HIGH);
      digitalWrite(pinG, HIGH);
      break;
    case 7:
      digitalWrite(pinA, HIGH);
      digitalWrite(pinB, HIGH);
      digitalWrite(pinC, HIGH);
      digitalWrite(pinD, LOW);
      digitalWrite(pinE, LOW);
      digitalWrite(pinF, LOW);
      digitalWrite(pinG, LOW);
      break;
    case 8:
      digitalWrite(pinA, HIGH);
      digitalWrite(pinB, HIGH);
      digitalWrite(pinC, HIGH);
      digitalWrite(pinD, HIGH);
      digitalWrite(pinE, HIGH);
      digitalWrite(pinF, HIGH);
      digitalWrite(pinG, HIGH);
      break;
    case 9:
      digitalWrite(pinA, HIGH);
      digitalWrite(pinB, HIGH);
      digitalWrite(pinC, HIGH);
      digitalWrite(pinD, HIGH);
      digitalWrite(pinE, LOW);
      digitalWrite(pinF, HIGH);
      digitalWrite(pinG, HIGH);
      break;
  }
}

void pick_digit(int digit) {
  digitalWrite(DIG1, HIGH);
  digitalWrite(DIG2, HIGH);
  digitalWrite(DIG3, HIGH);

  switch (digit) {
    case 1: 
      digitalWrite(DIG1, LOW); 
      break;
    case 2: 
      digitalWrite(DIG2, LOW); 
      break;
    default: 
      digitalWrite(DIG3, LOW); 
      break;
  }
}

void seven_segment(int number) {
  unsigned char hundreds = int (number / 100);
  unsigned char tens     = int ((number / 10) % 10);
  unsigned char ones     = int (number % 10);

  //123
  choose_digit(hundreds);
  pick_digit(1);
  delay(time);

  choose_digit(tens);
  pick_digit(2);
  delay(time);

  choose_digit(ones);
  pick_digit(3);
  delay(time);
}

Try it in Wokwi simulation:

1 Like

What have you tried? You weight sketch just returns the units, not the actual weight of anything on the scale. Look at the examples that come with the HX71 library.

Thank you very much, my brother
Now, can I add a modification to the code?

The modification is:
For example, I put a weight on the cell of 50 kg. When the weight is removed, I do not want the screen to return to the value of 0. Rather, I want the screen to zero after a period of time, for example 10 seconds.

is that possible?

Yes, of course is it possible.
We use millis() for that. This is the starting page for millis(): https://docs.arduino.cc/built-in-examples/digital/BlinkWithoutDelay.

My sketch is not working well.
Can you make a test to read the HX711, and another test for the 7-segment display ?

Hello dear
Tested to read the HX711 separately from the screen works well
Another test of the 7-segment screen separately from the HX711 works well
But test the built-in code you sent me

The code you sent me appears to be for a maximum of 2.35 kg and is displayed on the screen as 987.

The load cell I have is 280 kg
According to the simulation website

When the weight is set at 5 kg, the result becomes 2100, which means a number consisting of 4 digits and a screen consisting of 7 parts, and I only have 3 digits, so the value of the screen will be 000.
When the weight is determined as 2.35 kg, the result becomes 987, meaning a 3-digit number
I want to display the weight of 280 kg on my 3-digit screen, where the output on the screen is 999

Here is the explanation of the HX711 library: https://github.com/bogde/HX711#features
I it possible to do that calibration in Wokwi, but I think that I do something wrong, I can not find the calibration factor :cold_sweat:

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.