Arduino+HX711+SD,电桥传感器,数据采集与SD卡存储
在坛子里学习了很久,最近做了一个电桥传感器的数据采集记录仪,留个纪念。引用帖,在此对作者表示感谢:
基于24位AD转换模块HX711的重量称量实验(已补充皮重存储,线性温度漂移修正)
http://www.geek-workshop.com/thread-2315-1-1.html
arduino学习笔记18 - SD卡读写实验
http://www.geek-workshop.com/thread-104-1-1.html
硬件含nano mini板、SD模块、hx711模块以及13块钱2个还包邮的2G miniSD卡
详细过程我就不再累述,大部分在代码里有注释
#include <SPI.h>
#include <SD.h>
#include <HX711.h>
const int chipSelect = 4;
void(*resetFunc)(void) = 0;
Sd2Card card;
SdVolume volume;
SdFile root;
//--------------------记录模式
void logger()
{
// Open serial communications and wait for port to open:
Serial.begin(115200);
while (!Serial) {
; // wait for serial port to connect. Needed for Leonardo only
}
// see if the card is present and can be initialized:
while (!SD.begin(chipSelect)) {
Serial.println("Card failed, or not present");
// don't do anything more:
resetFunc();//异常重启
}
while (!card.init(SPI_HALF_SPEED, chipSelect)) {
Serial.println("initialization failed.");
resetFunc();//异常重启
}
// Now we will try to open the 'volume'/'partition' - it should be FAT16 or FAT32
while (!volume.init(card)) {
Serial.println("Could not find FAT16/FAT32 partition.\nMake sure you've formatted the card");
resetFunc();//异常重启
}
Serial.println("ready to log...");
delay(1000);
Serial.println("3...");
delay(1000);
Serial.println("2...");
delay(1000);
Serial.println("1...");
delay(1000);
HX711 hx_A(5, 6, 128, 1);//传感器A,D5,D6,最后一位为修正系数
HX711 hx_B(7, 8, 128, 1);//传感器B,D7,D8
hx_A.set_offset(169600);//传感器A偏移值
hx_B.set_offset(169600);//传感器B偏移值
//案例用的9,10好像mini板不支持
long sq = 0;
for (int n = 0; n < 99999999; n++) {
sq = millis() / 100 - 40;//序号以ms为单位,减去前边4s延时
long t_A = 0, t_B = 0;
t_A = hx_A.read();//直接读取原始数值
t_B = hx_B.read();
// t_A = hx_A.bias_read();//直接读取修正后数值
// t_B = hx_B.bias_read();
String rec = String(sq) + "," + String(t_A) + "," + String(t_B);
Serial.println(rec);
//---------------------------------------往SD里存储
File dataFile = SD.open("datalog.txt", FILE_WRITE);
//
if (dataFile) {
dataFile.println(rec);
dataFile.close();
}
else {
Serial.println("error opening datalog.txt");
resetFunc();//异常重启
}
delay(77);//延时,使得采集频率为10HZ
}
}
//--------------------------------------读取数据模式
void reader()
{
// Open serial communications and wait for port to open:
Serial.begin(115200);
while (!Serial) {
; // wait for serial port to connect. Needed for Leonardo only
}
// see if the card is present and can be initialized:
while (!SD.begin(chipSelect)) {
Serial.println("Card failed, or not present");
// don't do anything more:
resetFunc();//异常重启
}
while (!card.init(SPI_HALF_SPEED, chipSelect)) {
Serial.println("initialization failed.");
resetFunc();//异常重启
}
// Now we will try to open the 'volume'/'partition' - it should be FAT16 or FAT32
while (!volume.init(card)) {
Serial.println("Could not find FAT16/FAT32 partition.\nMake sure you've formatted the card");
resetFunc();//异常重启
}
// print the type and size of the first FAT-type volume
uint32_t volumesize;
volumesize = volume.blocksPerCluster(); // clusters are collections of blocks
volumesize *= volume.clusterCount(); // we'll have a lot of clusters
volumesize *= 512; // SD card blocks are always 512 bytes
volumesize /= 1024;
Serial.print("Volume size (Mbytes): ");
volumesize /= 1024;
Serial.println(volumesize);
Serial.println("\nFiles found on the card (name, date and size in bytes): ");
root.openRoot(volume);
// list all files in the card with date and size
root.ls(LS_R | LS_DATE | LS_SIZE);
Serial.println("ready to read...");
delay(1000);
Serial.println("3...");
delay(1000);
Serial.println("2...");
delay(1000);
Serial.println("1...");
delay(1000);
File dataFile = SD.open("DATALOG.TXT");
if (dataFile) {
while (dataFile.available()) {
Serial.write(dataFile.read());
}
dataFile.close();
Serial.println("download finished!");
//-------------------------------------------------------------------------
int buttonPin = 3; // D3串口高电平,删除数据
pinMode(buttonPin, INPUT);
int buttonState = 0;
buttonState = digitalRead(buttonPin);
Serial.println(buttonState);
while (buttonState == 1) {
Serial.println("Clean data in 5 sec.");
delay(5000);
SD.remove("datalog.txt");
Serial.println("Clean finished!");
return;
}
}
else {
Serial.println("error opening datalog.txt");
resetFunc();//异常重启
}
}
//---------------------------------------------------------------------------------
void setup() {
int buttonPin = 2; // D2高电平--logger模式,D2低电平--reader模式
int buttonState = 0;
pinMode(9, OUTPUT);
pinMode(10, OUTPUT);
digitalWrite(9, LOW);
digitalWrite(10, HIGH); //VCC与GND不够用,把D9,10口当电源
pinMode(14, OUTPUT);
pinMode(15, OUTPUT);
analogWrite(14, 0); //A0-5就是P14-19,模拟口可以直接当数字口用,涨姿势了~
analogWrite(15, 255); //VCC与GND不够用,把A0,1口当电源
pinMode(buttonPin, INPUT);
buttonState = digitalRead(buttonPin);
if (buttonState == HIGH) {
logger();
}
else {
reader();
}
}
//----------------
void loop()
{
}
补充几个注意点,也请大家验证
1、hx711的AB通道如果同时采集并存储进SD,延时会很厉害,大概有1S,达不到所需要的10HZ
2、案例里hx711用的D9,10,我的mini板不能用,运行时直接卡住,换D5,6,7,8解决
3、安例里用了for循环采10个取平均数,延时也很厉害,不知道是不是边读边存的原因
4、加的模块多了,常遇到5V和GND不够用,其实D和A口中大多数都能变通使用,但并不是全能用,有几口是特殊的,需看板子原理图
下一步准备再挂个蓝牙或wifi,当连通时开启reader模式,断开时logger模式,这样就能把令人崩溃的跳线扔掉。 牛人,学以致用。
先收藏,以后可能也弄一个。
你好 请问下你是怎么使用两个桥式传感器的连接和数据的读取的?我连接了连个传感器 可是只有一个能使用,传感器是没有问题的 你是分时复用的吗?商家和我说过分时复用可是具体怎么用不知道,还是你接的是两个HX711? 望解答
lala5 发表于 2017-3-30 23:25
你好 请问下你是怎么使用两个桥式传感器的连接和数据的读取的?我连接了连个传感器 可是只有一个能使用,传 ...
接的2个HX711 你好请问有HX711的库吗能不能发一下
页:
[1]