An introduction to electronic weighing
Filtering Data
Before displaying the data, some sort of filtering is usually required that removes fluctuations in the weight display caused by noise and vibration, but filtering comes at the cost of response time. The raw samples from the ADC take 100ms to acquire, so any form of low-pass filtering will slow down the response time into the region by fractions of a second. In the case of a workshop balance, a couple of seconds for the reading to settle is not a problem, but in other cases (e.g., a batch-weighing application), every second adds to production time and therefore costs money. Filtering and ultimate accuracy will always be a trade-off. In this application, 10 samples are simply averaged before the result is passed for display (Listing 3).
Listing 3
Averaging ADC Readings
01 #define SAMPLES 10 02 03 int32_t samples[SAMPLES]; 04 int32_t average = 0; 05 uint8_t sample = 0; 06 07 // main acquisition loop 08 while (true) 09 { 10 samples[sample++] = ADS1232_Read(); 11 if (sample == SAMPLES) 12 { 13 sample = 0; 14 } 15 16 average = 0; 17 for (uint8_t i = 0; i < SAMPLES; i++) 18 { 19 average += samples[i]; 20 } 21 22 average /= SAMPLES; 23 24 // pass the averaged data to the display here 25 }
Practical Precision
In terms of practical precision, you must keep a few things in mind. Of the theoretical 24 bits of precision available from the ADC, the datasheet itself admits that only 19 bits are useful at the highest gain setting. The load cell I have chosen has a maximum output of 4mV (1mV/V, with an excitation of 4V), so it uses only one fifth of the available range (20mV) of the ADC. That's about 100,000 counts. Practically, I have found I can achieve stable readings to 10,000 counts. For a 100g scale, that means one count is equivalent to 10mg. The current software limits that further, giving a resolution of 100mg (i.e., 1,000 counts), which seems more than adequate for the requirements of a general-purpose laboratory scale. There is certainly scope for better filtering and other techniques to improve the precision, but at some point the limitations of the (cheap) load cell will become the limiting factor.
Display Driver
The display has a three-wire serial interface. The chip select (CS) line must be asserted before data can be written, and once that is done, data is presented on the data (DAT) line, being clocked into the chip by asserting the write (WR) line and then negating it (Listing 4). The interface is too slow to keep up with the microcontroller at full speed, so busy-wait loops are necessary to slow the WR line pulses down to just over 3µs.
Listing 4
Writing a Single Bit to the Display
01 /** */ 02 static void LCD_WriteBit(bool bit) 03 { 04 // set the data line 05 HAL_GPIO_WritePin(LCD_DAT_GPIO_Port, LCD_DAT_Pin, bit); 06 07 // assert the WR line 08 HAL_GPIO_WritePin(LCD_NWR_GPIO_Port, LCD_NWR_Pin, 0); 09 10 // busy wait 11 for (uint8_t i = 0; i < 50; i++) 12 ; 13 14 // negate the WR line 15 HAL_GPIO_WritePin(LCD_NWR_GPIO_Port, LCD_NWR_Pin, 1); 16 17 // busy wait 18 for (uint8_t i = 0; i < 50; i++) 19 ; 20 }
The display chip has a small amount of internal memory, and the individual bits in this memory correspond to the different segments in the display characters, as well as decimal points and the battery state indicator. Each transaction with the chip is called a command, and the first three bits of that command determine the type of command. The rest of the transaction comprises an address in the internal memory and the data to be written.
The process of writing a complete digit involves conversion from the required ASCII digit to the memory bit pattern to illuminate the display correctly. This code is not complex, but quite long, so I won't include it here. I refer the interested reader to the source code on my GitHub page [11].
Buy this article as PDF
(incl. VAT)
Buy Linux Magazine
Subscribe to our Linux Newsletters
Find Linux and Open Source Jobs
Subscribe to our ADMIN Newsletters
Support Our Work
Linux Magazine content is made possible with support from readers like you. Please consider contributing when you’ve found an article to be beneficial.
News
-
Canonical Bumps LTS Support to 12 years
If you're worried that your Ubuntu LTS release won't be supported long enough to last, Canonical has a surprise for you in the form of 12 years of security coverage.
-
Fedora 40 Beta Released Soon
With the official release of Fedora 40 coming in April, it's almost time to download the beta and see what's new.
-
New Pentesting Distribution to Compete with Kali Linux
SnoopGod is now available for your testing needs
-
Juno Computers Launches Another Linux Laptop
If you're looking for a powerhouse laptop that runs Ubuntu, the Juno Computers Neptune 17 v6 should be on your radar.
-
ZorinOS 17.1 Released, Includes Improved Windows App Support
If you need or desire to run Windows applications on Linux, there's one distribution intent on making that easier for you and its new release further improves that feature.
-
Linux Market Share Surpasses 4% for the First Time
Look out Windows and macOS, Linux is on the rise and has even topped ChromeOS to become the fourth most widely used OS around the globe.
-
KDE’s Plasma 6 Officially Available
KDE’s Plasma 6.0 "Megarelease" has happened, and it's brimming with new features, polish, and performance.
-
Latest Version of Tails Unleashed
Tails 6.0 is based on Debian 12 and includes GNOME 43.
-
KDE Announces New Slimbook V with Plenty of Power and KDE’s Plasma 6
If you're a fan of KDE Plasma, you'll be thrilled to hear they've announced a new Slimbook with an AMD CPU and the latest version of KDE Plasma desktop.
-
Monthly Sponsorship Includes Early Access to elementary OS 8
If you want to get a glimpse of what's in the pipeline for elementary OS 8, just set up a monthly sponsorship to help fund its continued existence.