greygrey_
greygrey_
采纳率100%
2020-01-11 16:30

用arduino实现温湿度传感器dht11,求大佬帮我看看代码,怎样写才能实现读取传感器信号?

int DHTpin=2;
byte dat[5];
//------------------------------------------------------
byte read_data()
{ byte data;
  for(int i=0;i<8;i++)
  {
    if(digitalRead(DHTpin)==LOW)
    {
      while(digitalRead(DHTpin)==LOW) ;
      delayMicroseconds(30);
      if(digitalRead(DHTpin)==HIGH)
      data|=(1<<(7-1));
      while(digitalRead(DHTpin)==HIGH) ;
      }
    }
    return data;
  }
    //------------------------------------------------------
void start_test(){
  digitalWrite(DHTpin,LOW);
  delay(30);
  digitalWrite(DHTpin,HIGH);
  delayMicroseconds(40);
  pinMode(DHTpin,INPUT);
  while(digitalRead(DHTpin)==LOW) ;
  delayMicroseconds(80);
  for(int i=0;i<5;i++)
  dat[i]=read_data();
  pinMode(DHTpin,OUTPUT);
  digitalWrite(DHTpin,HIGH);
}
void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
  pinMode(DHTpin,OUTPUT);
}

void loop() {
  // put your main code here, to run repeatedly:
  start_test();
  for(int i=0;i<5;i++)
  {
    Serial.print("dat[");
    Serial.print(i);
    Serial.print("]=");
    Serial.print(dat[i]);
    Serial.print(' ');
  }
  delay(700);
}

为什么串口监视器上一直显示0图片说明
read_data()里的代码该怎么写?

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

2条回答

  • weixin_45800443 weixin_45800443 1年前

    Serial.print(dat[i],DEC);
    你读到的数据还是byte类型,用DEC转成十进制就好了。我也是刚用这个

    点赞 评论 复制链接分享
  • dashicaineng 蔡能教授,网站特聘专家 1年前