python读取arduino串口数据_从Arduino项目读取串行数据(pySerial)

//Exampletesting sketchforvarious DHT humidity/temperature sensors//Writtenby ladyada,public domain#include "DHT.h"#define DHTPIN 2 // what pin we're connected to//Uncommentwhatever type you're using!#define DHTTYPE DHT11 // DHT 11//#define DHTTYPE DHT22 // DHT 22 (AM2302)//#define DHTTYPE DHT21 // DHT 21 (AM2301)// Connect pin 1 (on the left) of the sensor to +5V// NOTE: If using a board with 3.3V logic like an Arduino Due connect pin 1// to 3.3V instead of 5V!// Connect pin 2 of the sensor to whatever your DHTPIN is// Connect pin 4 (on the right) of the sensor to GROUND// Connect a 10K resistor from pin 2 (data) to pin 1 (power) of the sensor// Initialize DHT sensor for normal 16mhz ArduinoDHT dht(DHTPIN, DHTTYPE);// NOTE: For working with a faster chip, like an Arduino Due or Teensy, you// might need to increase the threshold for cycle counts considered a 1 or 0.// You can do this by passing a 3rd parameter for this threshold. It's a bit//of fiddling to find the right value,butingeneral the faster the CPU the//higher the value.Thedefaultfora16mhzAVRisa value of6.Foran//ArduinoDuethat runs at84mhza value of30works.//Exampleto initialize DHT sensorforArduinoDue://DHT dht(DHTPIN,DHTTYPE,30);void setup(){Serial.begin(115200);Serial.println("DHTxx test!");dht.begin();}void loop(){//Waita few seconds between measurements.delay(2000);//Readingtemperatureorhumidity takes about250milliseconds!//Sensorreadings may also be up to2seconds'old'(its a very slow sensor)float h=dht.readHumidity();//ReadtemperatureasCelsiusfloat t=dht.readTemperature();//ReadtemperatureasFahrenheit//Checkifany reads failedandexit early(totryagain).if(isnan(h)||isnan(t)){Serial.println("Failed to read from DHT sensor!");return;}Serial.print("Humidity: ");Serial.print(h);Serial.print(" ");Serial.print("Temperature: ");Serial.print(t);Serial.println(" ");}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值