如何将重量传感器 HX711 与 Arduino 一起使用

How to use a Weight Sensor / Load Cell + HX711 with an Arduino

原文

img

OVERVIEW

We’ve all used a scale to determine the weight of something at some point in our lives.

Using a Load Cell or Weight sensor you can add this capability to your Arduino projects.

In this tutorial we will see how to connect, calibrate and display the weight on a small OLED display, and by using a rotary encoder we will be able to change the units from: grams, kg or pounds.

We will be using the HX711 ADC converter board to amplify the signal received from the weight sensor.

This amplifier enables the Arduino to detect the changes in resistance from the load cell.

PARTS USED

1KG Load Cell

Rotary Encoder Module

HX711 Module

LOAD CELL and HX711 INTRODUCTION

img

So what is a Load Cell?

They are many types of Load Cell, but today we will be using the straight bar type.

When pressure or a load is applied, the electrical resistance will change in response to this applied pressure and by taking this information and after some calibration we can determine the precise weight.

Load cell like this one come in different weight limits, the one we will be using today is rated up to 1KG, but you can get others that will support more weight if needed.

What about this HX711?

img

The changes in electrical resistance provided by the Load Cell need to be amplified so that they can be read by an Arduino.

That’s exactly what the HX711 board does.

It reads the information from the Load Cell, amplifies the signals and then sends it to the Arduino for processing.

Common Load cells have four-wire to connect the HX711 to a microcontroller like the Arduino.

  • Red (VCC)
  • Black (GND)
  • Data
  • Clock

To connect the HX711 to the Arduino you only need 2 pins (Clock and Data).

On the opposite side you can see the connection for the Load cell.

We will be using a simple Arduino library to communicate with the HX711 that provides a calibration and Tare (reset) feature to easily determine the weight of an object.

LOAD CELL SETUP

The bar type we are using has different screws holes on the bottom and the top.

To it up as a scale, you attach the bottom left or right screw holes to a table or stable object, and then attach a piece of platic or any other material to serve as the scale to put objects on, to the top opposite screw holes.

**Each bar type has an arrow that indicate the direction of deflection and should be positioned the correct way for your project.You can see a simple setup here:

img

This setup is for using the Load Cell has a simple scale, but you could also connect it the detect deflection between two object if you wanted.

CONNECTIONS

img

The connections for this tutorial are pretty straightforward:

The 5V and GND of the UNO is connected to each modules VCC and GND.

Since the OLED display uses I2C we are using pins A4 (SDA), A5 (SCL) on the UNO to connect it.

The rotary encoder is connected to these pins: CLK to pin 2, DT to pin 3 and SW to pin 4.The HX711 is connected to: DATA to pin 4 and CLOCK to pin 5.

Depending on the HX711 your purchase it may have different connections: If it has color designation, then connect the corrresponding wires from the Load Cell to the HX711.

In my case my HX711 had these connections:

E+ : Connected to RED of the Load Cell E- : Connected to BLACK

A- : Connected to GREEN A+ : Connected to WHITE

B- : no connections B+ : no connections

THE CODE

We are using a library written for the HX711 to communicate with the Arduino, you can find a link to download the library at the bottom of this page.

The first code is used to get the calibration factor for a particular Load cell, this will have to be run for each Load Cell you own, since each one is a little different.

The second code uses that calibration factor to print out the correct weight on the OLED display.

As always for more information please have a look at the tutorial video.

/* Calibration sketch for HX711 */
 
#include "HX711.h"  // Library needed to communicate with HX711 https://github.com/bogde/HX711
 
#define DOUT  6  // Arduino pin 6 connect to HX711 DOUT
#define CLK  5  //  Arduino pin 5 connect to HX711 CLK
 
HX711 scale(DOUT, CLK);  // Init of library

void setup() {
  Serial.begin(9600);
  scale.set_scale();  // Start scale
  scale.tare();       // Reset scale to zero
}

void loop() {
  float current_weight=scale.get_units(20);  // get average of 20 scale readings
  float scale_factor=(current_weight/0.145);  // divide the result by a known weight
  Serial.println(scale_factor);  // Print the scale factor to use
}
/* Arduino Scale using HX711 and Load Cell
 
Created by Yvan / https://Brainy-Bits.com

This code is in the public domain...

You can: copy it, use it, modify it, share it or just plain ignore it!
Thx!
*/


#include "U8glib.h"   // Library for Oled display https://github.com/olikraus/u8glib/
#include "HX711.h"    // Library for Load Cell amplifier board

volatile boolean TurnDetected;  // variable used to detect rotation of Rotary encoder
volatile int Rotary_Flag=0;     // flag to indicate rotation as occured

// Rotary Encoder Module connections
#define RotaryCLK 2   // Rotary encoder CLK pin connected to pin 2 of Arduino
#define RotaryDT 3    // Rotary encoder DT pin connected to pin 3
#define RotarySW 4    // Rotary encoder Switch pin connected to pin 4

// HX711 Module connections
#define CLK 5   // CLK of HX711 connected to pin 5 of Arduino
#define DOUT 6  // DOUT of HX711 connected to pin 6 of Arduino

int reset_screen_counter=0;      // Variable used to decide what to display on Oled
volatile int current_units=0;    // Used to select which measuring unit to use (KG,Grams,Pounds)
float unit_conversion;           // Used to convert between measuring units
int decimal_place;               // how many decimal number to display

HX711 scale(DOUT, CLK);  // Init of the HX711

U8GLIB_SSD1306_128X32 u8g(U8G_I2C_OPT_FAST);	// Init of the OLED


// Interrupt routine runs if Rotation detected from Rotary encoder
void rotarydetect ()  {
Rotary_Flag=1; // Set Rotary flag from 0 to 1
delay(500);
}


// Used to change the measurement units (0=grams, 1=KG, 2=pounds)
void change_units ()  { 
  if (current_units == 0) current_units=1;
  else if (current_units == 1) current_units=2;
  else if (current_units == 2) current_units=0;
}


// Run at Startup and when Resetting with Rotary encoder switch
void startupscreen(void) {
  u8g.setFont(u8g_font_unifont);
  u8g.firstPage(); 
  do {
    u8g.drawStr( 0, 10, "Clear Scale");
    u8g.drawStr( 0, 28, "Click to zero...");
  } while( u8g.nextPage() );
}

// Reset Scale to zero
void tare_scale(void) {
  scale.set_scale(-1073000);  //Calibration Factor obtained from calibration sketch
  scale.tare();             //Reset the scale to 0  
}


// Start Displaying information on OLED
void start_scale(void) {
  char temp_current_units[15];  // Needed to store String to Char conversion
  String KG="KG";
  String GRAMS="GRAMS";
  String LBS="POUNDS";
  
  if (current_units == 0) {                     // 0 = grams
    GRAMS.toCharArray(temp_current_units, 15);  // Convert String to Char for OLED display
    unit_conversion=1000;                        // conversion value for grams
    decimal_place=0;                            // how many decimal place numbers to display
  } else if (current_units == 1) {              // 1 = Kilograms
    KG.toCharArray(temp_current_units, 15);
    unit_conversion=1;
    decimal_place=3;
  } else {                                      // else 2 = Pounds
    LBS.toCharArray(temp_current_units, 15);
    unit_conversion=2.2046226218;
    decimal_place=3;
  }
  
    u8g.setFont(u8g_font_unifont);
    u8g.firstPage(); 
      do {
        u8g.drawStr( 0, 10, temp_current_units);  // Display the current measurement unit
        u8g.setPrintPos(38, 28);
        u8g.print(scale.get_units(3)*unit_conversion, decimal_place);  // Display the average of 3 scale value reading
      } while( u8g.nextPage() );
}



void setup(void) {
  
  // Set pinmode for Rotary encoder pins
  pinMode(RotarySW,INPUT_PULLUP);
  pinMode(RotaryCLK,INPUT_PULLUP);
  pinMode(RotaryDT,INPUT_PULLUP);  

  // Attach interrupt 0 (Pin 2 on UNO) to the Rotary Encoder
  attachInterrupt (0,rotarydetect,RISING);   // interrupt 0 always connected to pin 2 on Arduino UNO

  // Rotate screen 180 degrees on OLED, if required
  u8g.setRot180();

  // Set color of OLED to Monochrome
  u8g.setColorIndex(1);

  // Select font to use
  u8g.setFont(u8g_font_unifont);

  String start_count_string="Starting up....";  // Message to display at Startup
  char start_count[15];  // Used to String to Char conversion
  
  // Loop to display counting dots
  for (int x=12; x < 16; x++) {  // Select the first 12 to 16 character of String
    start_count_string.toCharArray(start_count, x);
    u8g.firstPage(); 
    do {
      u8g.drawStr( 0, 10, "ARDUINO SCALE");
      u8g.drawStr( 0, 28, start_count);
    } while( u8g.nextPage() );
    delay(500);  // Delay between dots
  }
}


void loop(void) {
  
// If Switch is pressed on Rotary Encoder
  if (!digitalRead(RotarySW)) {       // Check to see which action to take
    if(reset_screen_counter == 1) {   
      tare_scale();                   // 1 = zero and start scale
      reset_screen_counter=2;
      delay(500);
    } else {
      if(reset_screen_counter == 2) { // 2 = Scale already started so restart from begining
        reset_screen_counter=0;
        delay(500); 
      }
    }
  }

// If Rotation was detected
  if (Rotary_Flag == 1) {
    change_units();  // change the measuring units
    Rotary_Flag=0;   // reset flag to zero
  }

// If system was just started display intro screen  
  if (reset_screen_counter == 0) {    
    startupscreen();
  reset_screen_counter=1;  // set to 1 and wait for Rotary click to zero scale
  }
 
// if zero (tare) of scale as occured start display of weight
  if (reset_screen_counter == 2) {
          start_scale();
  }
}

TUTORIAL VIDEO

DOWNLOAD

Copy and Paste the above code/sketch in your Arduino IDE software.

Donwload the HX711 library here:

https://github.com/bogde/HX711

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:/a/740592.html

如若内容造成侵权/违法违规/事实不符,请联系我们进行投诉反馈qq邮箱809451989@qq.com,一经查实,立即删除!

相关文章

【面试题】面试官:判断图是否有环?_数据结构复试问题 有向图是否有环

type: NODE;name: string;[x: string]: any; }; [x: string]: any;}; export type Data Node | Edge; 复制代码 * 测试数据如下const data: Data[] [ { id: ‘1’, data: { type: ‘NODE’, name: ‘节点1’ } }, { id: ‘2’, data: { type: ‘NODE’, name: ‘节点2’ } },…

【kaggle数据集无法下载解决办法】

kaggle数据集无法下载的解决办法 当我们在做机器学习相关问题的时候&#xff0c;需要到kaggle网站上下载数据集&#xff0c;但是很多时候速度很慢或者连接超时等问题&#xff0c;此时解决办法如下&#xff1a; 在本地安装Kaggle API包 打开终端输入如下指令&#xff1a; pip i…

【NPS】哑终端设备如何实现域VLAN动态分配

在【NPS】微软NPS配置802.1x&#xff0c;验证域账号&#xff0c;动态分配VLAN&#xff08;有线网络续篇&#xff09;中&#xff0c;已经通过C3PL策略配置实现了802.1x验证没有通过时&#xff0c;自动分配一个Guest VLAN&#xff0c;以确保用户至少能够访问基本的网络服务。问题…

使用ChatGPT进行数据分析和可视化,12个专业顶级提示词指令,轻松上手使用

大家好&#xff0c;感谢关注。我是七哥&#xff0c;一个在高校里不务正业&#xff0c;折腾学术科研AI实操的学术人。可以和我&#xff08;yida985&#xff09;交流学术写作或ChatGPT等AI领域相关问题&#xff0c;多多交流&#xff0c;相互成就&#xff0c;共同进步。 高级学术…

【Java核心技术13】Java中的构造器与析构器:深入解析与代码示例

引言 所有文章均为原创验证&#xff0c;您随手的 关注、点赞、收藏 是我创作最大的动力。 示例代码地址&#xff1a;https://gitee.com/code-in-java/csdn-blog.git 在面向对象编程语言中&#xff0c;构造器和析构器是类生命周期管理的关键部分。构造器负责初始化新创建的对象&…

邂逅Three.js探秘图形世界之美

可能了解过three.js等大型的3D 图形库同学都知道啊&#xff0c;学习3D技术都需要有图形学、线性代数、webgl等基础知识&#xff0c;以前读书学的线性代数足够扎实的话听这节课也会更容易理解&#xff0c;这是shader课程&#xff0c;希望能帮助你理解着色器&#xff0c;也面向第…

红队内网攻防渗透:内网渗透之内网对抗:横向移动篇域控系统提权NetLogonADCSPACKDC永恒之蓝CVE漏洞

红队内网攻防渗透 1. 内网横向移动1.1 横向移动-域控提权-CVE-2020-1472 NetLogon1.2 横向移动-域控提权-CVE-2021-422871.3 横向移动-域控提权-CVE-2022-269231.4 横向移动-系统漏洞-CVE-2017-01461.5 横向移动-域控提权-CVE-2014-63241. 内网横向移动 1、横向移动-域控提权-…

Excel 宏录制与VBA编程 —— 11、工作表及工作簿操作(附:Worksheets与Sheets区别)

代码1 - Worksheets与Sheets区别 Worksheets表示普通工作表;Sheets即可表示普通工作表也可表示图标工作表。 下面模块中代码结果是一样的,大家理解时可结合上面区别说明进行了解 Sub Test()Worksheets("Sheet1").Range("A1").Value 100Sheets("Sheet…

python桌面应用

py文件 import osimport wx import wx.html2class MyFrame(wx.Frame):def __init__(self, parent):wx.Frame.__init__(self, parent, title"启动啦", size(1000, 700))# 创建一个Web视图组件self.browser wx.html2.WebView.New(self)# 加载本地HTML文件# self.brow…

Python重拾

1.Python标识符规则 字母&#xff0c;下划线&#xff0c;数字&#xff1b;数字不开头&#xff1b;大小写区分&#xff1b;不能用保留字&#xff08;关键字&#xff09; 2.保留字有哪些 import keyword print(keyword.kwlist)[False, None, True, and,as, assert, async, await…

在 The Sandbox 体验韩剧《碰撞搜查线》的刺激!

风靡全球的韩国电视剧《碰撞搜查线》现已登陆 The Sandbox 元宇宙&#xff01; ASTORY 的电视剧《碰撞搜查线》以充满动作喜剧色彩的方式&#xff0c;讲述了一个交通犯罪调查小组打击公路上的恶棍的故事。该剧迅速成为 Disney 最受欢迎的节目之一&#xff01; 在 The Sandbox体…

CSS阴影优化气泡框样式

<body> <div class"pop">气泡框</div> </body>body{display: flex;justify-content: center;align-items: center;height: 100% } .pop{display: flex;justify-content: center;align-items: center;background: #409eff;width: 150px;heigh…

python+unity实现数字人跟随运动

效果如下 设计思路 1 python通过摄像头提取人物肢体关键点信息 2 通过UDP将获取到人体信息发送给Unity 3 unity将获取的的人物信息进行解析 4 将解析的数据赋值给模型骨架 代码获取

GNSS接收机的工作原理

GNSS接收机的工作原理如下&#xff1a; 信号接收&#xff1a;GNSS接收机通过天线接收来自卫星导航系统的信号&#xff0c;这些信号包含卫星的位置、时间和健康状态等信息。 信号处理&#xff1a;接收的信号首先经过前置放大器放大&#xff0c;然后经过滤波器滤除噪声。接收机会…

iptables(6)扩展匹配条件--tcp-flags、icmp

简介 前面我们已经介绍了不少的扩展模块,例如multiport、iprange、string、time、connlimit模块,但是在tcp扩展模块中只介绍了tcp扩展模块中的”--sport”与--dport”选项,并没有介绍”--tcp-flags”选项,那么这篇文章,我们就来认识一下tcp扩展模块中的”--tcp-flags”和i…

【linux kernel】一文总结linux输入子系统

文章目录 一、导读二、重要数据数据结构&#xff08;2-1&#xff09;struct input_dev&#xff08;2-2&#xff09;input_dev_list和input_handler_list&#xff08;2-3&#xff09;struct input_handler 三、input核心的初始化四、常用API五、输入设备驱动开发总结(1)查看输入…

Linux:基础IO(三.软硬链接、动态库和静态库、动精态库的制作和加载)

上次介绍了基础IO&#xff08;二&#xff09;&#xff1a;Linux&#xff1a;基础IO&#xff08;二.缓冲区、模拟一下缓冲区、详细讲解文件系统&#xff09; 文章目录 1.软硬链接1.1硬链接1.2软链接使用场景 2.动态库和静态库1.1回顾1.2静态库的制作和使用为什么要有库制作者角度…

搜维尔科技:「案例」NBA新科冠军与Xsens运动捕捉的缘分

北京时间昨日&#xff0c;凯尔特人在主场106比88击败独行侠&#xff0c;以总比分4比1获胜&#xff0c;夺得队史第18冠&#xff0c;超越湖人队&#xff08;17冠&#xff09;成为历史上夺冠次数最多的球队。凯尔特人队上一次夺冠还是在2007-2008赛季。 凯尔特人队主力Jayson Tat…

Kotlin明明很优秀,为啥没像Java那样火?

在开始前刚好我有一些资料&#xff0c;是我根据网友给的问题精心整理了一份「java的资料从专业入门到高级教程」&#xff0c; 点个关注在评论区回复“666”之后私信回复“666”&#xff0c;全部无偿共享给大家&#xff01;&#xff01;&#xff01;因为 kotlin 优化一件事&…

太全了吧?CISP全类别详细介绍,看完不迷惑

今天聊聊CISP&#xff0c;注册信息安全专业人员证。 很多人以为说CISP就是个证书&#xff0c;没这么简单&#xff0c;这里面区别可大了。 CISP根据工作领域和实际岗位需要&#xff0c;分为综合型、攻防领域、IT审计、软件开发、数据治理、电子取证和云安全领域等17项证书。 这么…