极客工坊

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 16822|回复: 6

Arduino+HX711+SD,电桥传感器,数据采集与SD卡存储

[复制链接]
发表于 2016-12-6 21:35:37 | 显示全部楼层 |阅读模式
在坛子里学习了很久,最近做了一个电桥传感器的数据采集记录仪,留个纪念。
引用帖,在此对作者表示感谢:
基于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卡
详细过程我就不再累述,大部分在代码里有注释


  1. #include <SPI.h>
  2. #include <SD.h>
  3. #include <HX711.h>
  4. const int chipSelect = 4;
  5. void(*resetFunc)(void) = 0;
  6. Sd2Card card;
  7. SdVolume volume;
  8. SdFile root;
  9. //--------------------记录模式
  10. void logger()
  11. {
  12.   // Open serial communications and wait for port to open:
  13.   Serial.begin(115200);
  14.   while (!Serial) {
  15.     ; // wait for serial port to connect. Needed for Leonardo only
  16.   }
  17.   // see if the card is present and can be initialized:
  18.   while (!SD.begin(chipSelect)) {
  19.     Serial.println("Card failed, or not present");
  20.     // don't do anything more:
  21.     resetFunc();//异常重启
  22.   }
  23.   while (!card.init(SPI_HALF_SPEED, chipSelect)) {
  24.     Serial.println("initialization failed.");
  25.     resetFunc();//异常重启
  26.   }
  27.   // Now we will try to open the 'volume'/'partition' - it should be FAT16 or FAT32
  28.   while (!volume.init(card)) {
  29.     Serial.println("Could not find FAT16/FAT32 partition.\nMake sure you've formatted the card");
  30.     resetFunc();//异常重启
  31.   }

  32.   Serial.println("ready to log...");
  33.   delay(1000);
  34.   Serial.println("3...");
  35.   delay(1000);
  36.   Serial.println("2...");
  37.   delay(1000);
  38.   Serial.println("1...");
  39.   delay(1000);

  40.   HX711 hx_A(5, 6, 128, 1);//传感器A,D5,D6,最后一位为修正系数
  41.   HX711 hx_B(7, 8, 128, 1);//传感器B,D7,D8
  42.   hx_A.set_offset(169600);//传感器A偏移值
  43.   hx_B.set_offset(169600);//传感器B偏移值
  44.   //案例用的9,10好像mini板不支持

  45.   long sq = 0;
  46.   for (int n = 0; n < 99999999; n++) {
  47.     sq = millis() / 100 - 40;//序号以ms为单位,减去前边4s延时
  48.     long t_A = 0, t_B = 0;

  49.     t_A = hx_A.read();//直接读取原始数值
  50.     t_B = hx_B.read();
  51.    
  52. //    t_A = hx_A.bias_read();//直接读取修正后数值
  53. //    t_B = hx_B.bias_read();


  54.     String rec = String(sq) + "," + String(t_A) + "," + String(t_B);

  55.     Serial.println(rec);
  56.     //---------------------------------------往SD里存储
  57.     File dataFile = SD.open("datalog.txt", FILE_WRITE);
  58.     //
  59.     if (dataFile) {
  60.       dataFile.println(rec);
  61.       dataFile.close();

  62.     }
  63.     else {
  64.       Serial.println("error opening datalog.txt");
  65.       resetFunc();//异常重启
  66.     }
  67.     delay(77);//延时,使得采集频率为10HZ
  68.   }
  69. }
  70. //--------------------------------------读取数据模式
  71. void reader()
  72. {

  73.   // Open serial communications and wait for port to open:
  74.   Serial.begin(115200);
  75.   while (!Serial) {
  76.     ; // wait for serial port to connect. Needed for Leonardo only
  77.   }
  78.   // see if the card is present and can be initialized:
  79.   while (!SD.begin(chipSelect)) {
  80.     Serial.println("Card failed, or not present");
  81.     // don't do anything more:
  82.     resetFunc();//异常重启
  83.   }
  84.   while (!card.init(SPI_HALF_SPEED, chipSelect)) {
  85.     Serial.println("initialization failed.");
  86.     resetFunc();//异常重启
  87.   }
  88.   // Now we will try to open the 'volume'/'partition' - it should be FAT16 or FAT32
  89.   while (!volume.init(card)) {
  90.     Serial.println("Could not find FAT16/FAT32 partition.\nMake sure you've formatted the card");
  91.     resetFunc();//异常重启
  92.   }

  93.   // print the type and size of the first FAT-type volume
  94.   uint32_t volumesize;
  95.   volumesize = volume.blocksPerCluster();    // clusters are collections of blocks
  96.   volumesize *= volume.clusterCount();       // we'll have a lot of clusters
  97.   volumesize *= 512;                            // SD card blocks are always 512 bytes
  98.   volumesize /= 1024;
  99.   Serial.print("Volume size (Mbytes): ");
  100.   volumesize /= 1024;
  101.   Serial.println(volumesize);
  102.   Serial.println("\nFiles found on the card (name, date and size in bytes): ");
  103.   root.openRoot(volume);
  104.   // list all files in the card with date and size
  105.   root.ls(LS_R | LS_DATE | LS_SIZE);

  106.   Serial.println("ready to read...");
  107.   delay(1000);
  108.   Serial.println("3...");
  109.   delay(1000);
  110.   Serial.println("2...");
  111.   delay(1000);
  112.   Serial.println("1...");
  113.   delay(1000);

  114.   File dataFile = SD.open("DATALOG.TXT");
  115.   if (dataFile) {
  116.     while (dataFile.available()) {
  117.       Serial.write(dataFile.read());
  118.     }
  119.     dataFile.close();
  120.     Serial.println("download finished!");
  121.     //-------------------------------------------------------------------------
  122.     int buttonPin = 3;     // D3串口高电平,删除数据
  123.     pinMode(buttonPin, INPUT);
  124.     int buttonState = 0;
  125.     buttonState = digitalRead(buttonPin);
  126.     Serial.println(buttonState);
  127.     while (buttonState == 1) {
  128.       Serial.println("Clean data in 5 sec.");
  129.       delay(5000);

  130.       SD.remove("datalog.txt");
  131.       Serial.println("Clean finished!");
  132.       return;
  133.     }
  134.   }
  135.   else {
  136.     Serial.println("error opening datalog.txt");
  137.     resetFunc();//异常重启
  138.   }
  139. }

  140. //---------------------------------------------------------------------------------

  141. void setup() {
  142.   int buttonPin = 2;     // D2高电平--logger模式,D2低电平--reader模式
  143.   int buttonState = 0;
  144.   pinMode(9, OUTPUT);
  145.   pinMode(10, OUTPUT);
  146.   digitalWrite(9, LOW);
  147.   digitalWrite(10, HIGH); //VCC与GND不够用,把D9,10口当电源

  148.   pinMode(14, OUTPUT);
  149.   pinMode(15, OUTPUT);
  150.   analogWrite(14, 0); //A0-5就是P14-19,模拟口可以直接当数字口用,涨姿势了~
  151.   analogWrite(15, 255); //VCC与GND不够用,把A0,1口当电源


  152.   pinMode(buttonPin, INPUT);
  153.   buttonState = digitalRead(buttonPin);
  154.   if (buttonState == HIGH) {
  155.     logger();
  156.   }
  157.   else {
  158.     reader();
  159.   }
  160. }

  161. //----------------
  162. void loop()
  163. {

  164. }
复制代码


回复

使用道具 举报

 楼主| 发表于 2016-12-6 21:48:46 | 显示全部楼层
补充几个注意点,也请大家验证
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模式,这样就能把令人崩溃的跳线扔掉。
回复 支持 反对

使用道具 举报

发表于 2016-12-7 08:09:17 | 显示全部楼层
牛人,学以致用。
先收藏,以后可能也弄一个。
回复 支持 反对

使用道具 举报

发表于 2017-3-30 23:25:04 | 显示全部楼层
你好 请问下你是怎么使用两个桥式传感器的连接和数据的读取的?我连接了连个传感器 可是只有一个能使用,传感器是没有问题的 你是分时复用的吗?商家和我说过分时复用  可是具体怎么用不知道,还是你接的是两个HX711? 望解答
回复 支持 反对

使用道具 举报

 楼主| 发表于 2017-5-18 13:59:33 | 显示全部楼层
lala5 发表于 2017-3-30 23:25
你好 请问下你是怎么使用两个桥式传感器的连接和数据的读取的?我连接了连个传感器 可是只有一个能使用,传 ...

接的2个HX711
回复 支持 反对

使用道具 举报

发表于 2017-11-22 21:47:12 | 显示全部楼层
你好  请问有HX711的库吗  能不能发一下
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则 需要先绑定手机号

Archiver|联系我们|极客工坊

GMT+8, 2024-3-29 13:56 , Processed in 0.045814 second(s), 21 queries .

Powered by Discuz! X3.4 Licensed

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表