Components and supplies
2N3906 PNP Transistor
4 Digit 7 Segment Common Anode Display
Arduino Uno Rev3
Shift register - TPIC6B595
Apps and platforms
Arduino IDE 2.0 (beta)
KiCad
Project description
Code
HX711 Weigh scale
cpp
Example code for a weigh scale using a 5 digit LED
1// Weigh scale up 10KG 2 3#include <avr/pgmspace.h> 4#include <avr/io.h> 5#include <SPI.h> 6#include <Wire.h> 7#include <HX711_ADC.h> 8 9const int tare_sw = 9; 10long int offset = 0; 11long int calfactor = 0; 12int digit = 0; 13 14const int clk = 13; // SRCK 15const int latch = 10; // RCK 16const int data = 11; // DIN 17const int OE = 14; // output enable 18const int digit0 = 2; 19const int digit1 = 3; 20const int digit2 = 4; 21const int digit3 = 5; 22const int digit4 = 6; 23 24HX711_ADC LoadCell(8, 7); // parameters: dt pin, sck pin 25 26// 7-segment digits 0-9 27// {Q0, Q1, Q2, Q3, Q4, Q5, Q6, Q7} --> {g, f, e, d, c, b, a, DP} 28byte ssddigits[10] = // array without decimal points on 29{ 30 B01111110, // 0 31 B00001100, // 1 32 B10110110, // 2 33 B10011110, // 3 34 B11001100, // 4 35 B11011010, // 5 36 B11111010, // 6 37 B01001110, // 7 38 B11111110, // 8 39 B11011110, // 9 40}; 41 42byte ssddigitsDP[10] = // array with decimal points on 43{ 44 B01111111, // 0 45 B00001101, // 1 46 B10110111, // 2 47 B10011111, // 3 48 B11001101, // 4 49 B11011011, // 5 50 B11111011, // 6 51 B01001111, // 7 52 B11111111, // 8 53 B11011111, // 9 54}; 55 56void setup() 57{ 58 pinMode (tare_sw, INPUT_PULLUP); 59 pinMode (clk, OUTPUT); 60 pinMode (latch, OUTPUT); 61 pinMode (data, OUTPUT); 62 pinMode (OE, OUTPUT); // use external pull-up 63 pinMode (digit0, OUTPUT); 64 pinMode (digit1, OUTPUT); 65 pinMode (digit2, OUTPUT); 66 pinMode (digit3, OUTPUT); 67 pinMode (digit4, OUTPUT); 68 //Serial.begin(9600); // for testing. Serial not connected in final version 69 LoadCell.begin(); // start connection to HX711 70 LoadCell.start(2000); // load cells gets 2000ms of time to stabilize 71 //calfactor = analogRead(1); 72 //LoadCell.setCalFactor(calfactor); 73 LoadCell.setCalFactor(201.4); // calibration factor for load cell => strongly dependent on your individual setup 74 LoadCell.tareNoDelay(); // cancel out weight of the plate attached to the sensor 75 digitalWrite(OE,LOW); // turn LED display on. OE / G is pulled up to +5V externally by 4.7K resistor. 76 //This prevents LED burnout if at power on / bootup random segments are lit (multiplexing code will not be running) 77} 78 79void loop() 80{ 81 LoadCell.update(); // retrieves data from the load cell 82 long int grams = LoadCell.getData() -offset; // get output value 83 //long int grams = 12345; // for testing 84 //Serial.println(grams); 85 86 static uint16_t btndbc = 0; 87 btndbc=(btndbc<<1) | digitalRead(tare_sw) | 0xe000; // debounce button 88 if (btndbc==0xf000) 89 90 { 91 delay(1); // wait 1 millisecond 92 offset = LoadCell.getData(); // read initial HX711 value and store it as offset 93 } 94 95 // get digit data. Digit 0 is leftmost 96 int dig4 = (grams % 10); // ones 97 int dig3 = (grams / 10) % 10; // tens 98 int dig2 = (grams / 100) % 10; // hundreds 99 int dig1 = (grams / 1000) % 10; // thousands 100 int dig0 = (grams / 10000) %10; // ten thousands 101 102 delay(1); 103 104 // turn all digits off by setting digit pin high on each loop through. 105 PORTD |= (1<<PORTD2); 106 PORTD |= (1<<PORTD3); 107 PORTD |= (1<<PORTD4); 108 PORTD |= (1<<PORTD5); 109 PORTD |= (1<<PORTD6); 110 111 if (grams <-9 || grams >10010) // show error if minus figure or scale is overloaded 112 113 { 114 switch (digit) 115 { 116 case 0: 117 SPI.beginTransaction(SPISettings(8000000, LSBFIRST, SPI_MODE0)); 118 PORTB &= ~(1<<PORTB2); // set latch LOW 119 SPI.transfer(B11110010); //E 120 PORTB |= (1<<PORTB2); // set latch HIGH 121 SPI.endTransaction(); 122 PORTD &= ~(1<<PORTD2); // set digit pin 0 LOW 123 delay(2); 124 break; 125 126 case 1: 127 SPI.beginTransaction(SPISettings(8000000, LSBFIRST, SPI_MODE0)); 128 PORTB &= ~(1<<PORTB2); // set latch LOW 129 SPI.transfer(B10100000); // r 130 PORTB |= (1<<PORTB2); // set latch HIGH 131 SPI.endTransaction(); 132 PORTD &= ~(1<<PORTD3); 133 delay(2); 134 break; 135 136 case 2: 137 SPI.beginTransaction(SPISettings(8000000, LSBFIRST, SPI_MODE0)); 138 PORTB &= ~(1<<PORTB2); // set latch LOW 139 SPI.transfer(B10100000); //r 140 PORTB |= (1<<PORTB2); // set latch HIGH 141 SPI.endTransaction(); 142 PORTD &= ~(1<<PORTD4); 143 delay(2); 144 break; 145 146 case 3: 147 SPI.beginTransaction(SPISettings(8000000, LSBFIRST, SPI_MODE0)); 148 PORTB &= ~(1<<PORTB2); // set latch LOW 149 SPI.transfer(B10111000); // o 150 PORTB |= (1<<PORTB2); // set latch HIGH 151 SPI.endTransaction(); 152 PORTD &= ~(1<<PORTD5); 153 delay(2); 154 break; 155 156 case 4: 157 SPI.beginTransaction(SPISettings(8000000, LSBFIRST, SPI_MODE0)); 158 PORTB &= ~(1<<PORTB2); // set latch LOW 159 SPI.transfer(B10100000); //r 160 PORTB |= (1<<PORTB2); // set latch HIGH 161 SPI.endTransaction(); 162 PORTD &= ~(1<<PORTD6); 163 delay(2); 164 break; 165 } 166 167 digit++; 168 169 if (digit == 5) 170 { 171 digit = 0; 172 } 173 } 174 175 else // display normally 176 { 177 switch (digit) 178 { 179 case 0: 180 SPI.beginTransaction(SPISettings(8000000, LSBFIRST, SPI_MODE0)); 181 PORTB &= ~(1<<PORTB2); // set latch LOW 182 SPI.transfer(ssddigits[dig0]); 183 PORTB |= (1<<PORTB2); // set latch HIGH 184 SPI.endTransaction(); 185 PORTD &= ~(1<<PORTD2); // set digit pin 0 LOW 186 delay(2); 187 break; 188 189 case 1: 190 SPI.beginTransaction(SPISettings(8000000, LSBFIRST, SPI_MODE0)); 191 PORTB &= ~(1<<PORTB2); // set latch LOW 192 SPI.transfer(ssddigitsDP[dig1]); // put a decimal point here. HX711 output is in grams and display will show in KG. 193 PORTB |= (1<<PORTB2); // set latch HIGH 194 SPI.endTransaction(); 195 PORTD &= ~(1<<PORTD3); 196 delay(2); 197 break; 198 199 case 2: 200 SPI.beginTransaction(SPISettings(8000000, LSBFIRST, SPI_MODE0)); 201 PORTB &= ~(1<<PORTB2); // set latch LOW 202 SPI.transfer(ssddigits[dig2]); 203 PORTB |= (1<<PORTB2); // set latch HIGH 204 SPI.endTransaction(); 205 PORTD &= ~(1<<PORTD4); 206 delay(2); 207 break; 208 209 case 3: 210 SPI.beginTransaction(SPISettings(8000000, LSBFIRST, SPI_MODE0)); 211 PORTB &= ~(1<<PORTB2); // set latch LOW 212 SPI.transfer(ssddigits[dig3]); 213 PORTB |= (1<<PORTB2); // set latch HIGH 214 SPI.endTransaction(); 215 PORTD &= ~(1<<PORTD5); 216 delay(2); 217 break; 218 219 case 4: 220 SPI.beginTransaction(SPISettings(8000000, LSBFIRST, SPI_MODE0)); 221 PORTB &= ~(1<<PORTB2); // set latch LOW 222 SPI.transfer(ssddigits[dig4]); 223 PORTB |= (1<<PORTB2); // set latch HIGH 224 SPI.endTransaction(); 225 PORTD &= ~(1<<PORTD6); 226 delay(2); 227 break; 228 } 229 230 digit++; 231 232 if (digit == 5) 233 { 234 digit = 0; 235 } 236 } 237}
Comments
Only logged in users can leave comments
adrian-smith31
1 Followers
•4 Projects
Project of the month
November 2023
winner
2
0