2017-11-28 22:09:02 wybliw 阅读数 2131
  • arduino实战

    学习如何在arduino中使用各种传感器,包括人体红外传感器,超声波传感器,舵机控制,温湿度传感器,激光接收传感器等,空气质量传感器,wifi模块等....课程内容会不断的更新增加,只要发现比较有趣的传感器就会做对应的实战视频

    405 人正在学习 去看看 陈贤能



const int echopin = 3; // echo接3端口
const int trigpin = 2; // trig接2端口
const int led = 13;
//const int led = A1;
const int DIG1 = 4;
const int DIG2 = 12;
//A = 5;B = 6;C = 7;D = 8;E = 9;F = 10;G = 11;

byte SEG7[10][7] = {
  { 0, 0, 0, 0, 0, 0, 1}, // = 0
  { 1, 0, 0, 1, 1, 1, 1}, // = 1
  { 0, 0, 1, 0, 0, 1, 0}, // = 2
  { 0, 0, 0, 0, 1, 1, 0}, // = 3
  { 1, 0, 0, 1, 1, 0, 0}, // = 4
  { 0, 1, 0, 0, 1, 0, 0}, // = 5
  { 0, 1, 0, 0, 0, 0, 0}, // = 6
  { 0, 0, 0, 1, 1, 1, 1}, // = 7
  { 0, 0, 0, 0, 0, 0, 0}, // = 8
  { 0, 0, 0, 0, 1, 0, 0}  // = 9
};

void setup()
{
  Serial.begin(9600);
  pinMode(echopin, INPUT); //设定echo为输入模式
  pinMode(trigpin, OUTPUT); //设定trig为输出模式
  for (int i = 4; i < 13; i++)
    pinMode(i, OUTPUT);
  pinMode(led, OUTPUT);
}

void loop()
{
  digitalWrite(trigpin, LOW);
  delayMicroseconds(2);
  digitalWrite(trigpin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigpin, LOW); //发一个10ms的高脉冲去触发TrigPin
  int pin=5;
  int distance = pulseIn(echopin, HIGH); //接收高电平时间
  distance = distance / 58.0; //计算距离
  Serial.print(distance);  //输出距离
  Serial.println("cm");  //输出单位
  
  if (distance < 100) {
    int shi = distance / 10;
    int ge = distance % 10;
   
    //ge
    digitalWrite(DIG2, LOW);
    digitalWrite(DIG1, HIGH);
    if (ge == 0)  {
      pin = 5;
      for (int s = 0; s < 7; s++) {
        digitalWrite(pin, SEG7[0][s]);
        pin++;
      }
    } else if (ge == 1)  {
      pin = 5;
      for (int s = 0; s < 7; s++) {
        digitalWrite(pin, SEG7[1][s]);
        pin++;
      }
    } else if (ge == 2) {
      pin = 5;
      for (int s = 0; s < 7; s++) {
        digitalWrite(pin, SEG7[2][s]);
        pin++;
      }
    } else if (ge == 3) {
      pin = 5;
      for (int s = 0; s < 7; s++) {
        digitalWrite(pin, SEG7[3][s]);
        pin++;
      }
    } else if (ge == 4) {
      pin = 5;
      for (int s = 0; s < 7; s++) {
        digitalWrite(pin, SEG7[4][s]);
        pin++;
      }
    } else if (ge == 5) {
      pin = 5;
      for (int s = 0; s < 7; s++) {
        digitalWrite(pin, SEG7[5][s]);
        pin++;
      }
    } else if (ge == 6) {
      pin = 5;
      for (int s = 0; s < 7; s++) {
        digitalWrite(pin, SEG7[6][s]);
        pin++;
      }
    } else if (ge == 7) {
      pin = 5;
      for (int s = 0; s < 7; s++) {
        digitalWrite(pin, SEG7[7][s]);
        pin++;
      }
    } else if (ge == 8) {
      pin = 5;
      for (int s = 0; s < 7; s++) {
        digitalWrite(pin, SEG7[8][s]);
        pin++;
      }
    } else if (ge == 9) {
      pin = 5;
      for (int s = 0; s < 7; s++) {
        digitalWrite(pin, SEG7[9][s]);
        pin++;
      }
    }
    delay(10);
    //shi
    digitalWrite(DIG1, LOW);
    digitalWrite(DIG2, HIGH);
       if (shi == 0)  {
      pin = 5;
      for (int s = 0; s < 7; s++) {
        digitalWrite(pin, SEG7[0][s]);
        pin++;
      }
    } else if (shi == 1)  {
      pin = 5;
      for (int s = 0; s < 7; s++) {
        digitalWrite(pin, SEG7[1][s]);
        pin++;
      }
    } else if (shi == 2) {
      pin = 5;
      for (int s = 0; s < 7; s++) {
        digitalWrite(pin, SEG7[2][s]);
        pin++;
      }
    } else if (shi == 3) {
      pin = 5;
      for (int s = 0; s < 7; s++) {
        digitalWrite(pin, SEG7[3][s]);
        pin++;
      }
    } else if (shi == 4) {
      pin = 5;
      for (int s = 0; s < 7; s++) {
        digitalWrite(pin, SEG7[4][s]);
        pin++;
      }
    } else if (shi == 5) {
      pin = 5;
      for (int s = 0; s < 7; s++) {
        digitalWrite(pin, SEG7[5][s]);
        pin++;
      }
    } else if (shi == 6) {
      pin = 5;
      for (int s = 0; s < 7; s++) {
        digitalWrite(pin, SEG7[6][s]);
        pin++;
      }
    } else if (shi == 7) {
      pin = 5;
      for (int s = 0; s < 7; s++) {
        digitalWrite(pin, SEG7[7][s]);
        pin++;
      }
    } else if (shi == 8) {
      pin = 5;
      for (int s = 0; s < 7; s++) {
        digitalWrite(pin, SEG7[8][s]);
        pin++;
      }
    } else if (shi == 9) {
      pin = 5;
      for (int s = 0; s < 7; s++) {
        digitalWrite(pin, SEG7[9][s]);
        pin++;
      }
    }
     
  } else {
    digitalWrite(DIG1, HIGH);
    digitalWrite(DIG2, HIGH);
    for (int s = 0; s < 6; s++) {
        digitalWrite(pin, HIGH);
        pin++;
    }
    digitalWrite(pin, LOW);
  }

  //距离小于10cm时,led写入高电平
  if (distance <= 20)
  {
    //digitalWrite(led, HIGH);
    analogWrite(A1, 255);
    //tone(led,1000,1000);
    //delay(15);
    tone(led,2000,100);
    //delay(15);
    //noTone(led);
  } else {
    analogWrite(A1, 0);
    //digitalWrite(led, LOW);
  }

  delay(15);   //循环间隔60uS
}


2017-05-16 11:00:40 wybliw 阅读数 1797
  • arduino实战

    学习如何在arduino中使用各种传感器,包括人体红外传感器,超声波传感器,舵机控制,温湿度传感器,激光接收传感器等,空气质量传感器,wifi模块等....课程内容会不断的更新增加,只要发现比较有趣的传感器就会做对应的实战视频

    405 人正在学习 去看看 陈贤能


硬件图:






程序调试图片:



Arduino源代码:


const int echopin = 3; // echo接3端口
const int trigpin = 2; // trig接2端口
const int led = 13;
//const int led = A1;
const int DIG1 = 4;
const int DIG2 = 12;
//A = 5;B = 6;C = 7;D = 8;E = 9;F = 10;G = 11;

byte SEG7[10][7] = {
  { 0, 0, 0, 0, 0, 0, 1}, // = 0
  { 1, 0, 0, 1, 1, 1, 1}, // = 1
  { 0, 0, 1, 0, 0, 1, 0}, // = 2
  { 0, 0, 0, 0, 1, 1, 0}, // = 3
  { 1, 0, 0, 1, 1, 0, 0}, // = 4
  { 0, 1, 0, 0, 1, 0, 0}, // = 5
  { 0, 1, 0, 0, 0, 0, 0}, // = 6
  { 0, 0, 0, 1, 1, 1, 1}, // = 7
  { 0, 0, 0, 0, 0, 0, 0}, // = 8
  { 0, 0, 0, 0, 1, 0, 0}  // = 9
};

void setup()
{
  Serial.begin(9600);
  pinMode(echopin, INPUT); //设定echo为输入模式
  pinMode(trigpin, OUTPUT); //设定trig为输出模式
  for (int i = 4; i < 13; i++)
    pinMode(i, OUTPUT);
  pinMode(led, OUTPUT);
}

void loop()
{
  digitalWrite(trigpin, LOW);
  delayMicroseconds(2);
  digitalWrite(trigpin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigpin, LOW); //发一个10ms的高脉冲去触发TrigPin
  int pin=5;
  int distance = pulseIn(echopin, HIGH); //接收高电平时间
  distance = distance / 58.0; //计算距离
  Serial.print(distance);  //输出距离
  Serial.println("cm");  //输出单位
  
  if (distance < 100) {
    int shi = distance / 10;
    int ge = distance % 10;
    
    //ge
    digitalWrite(DIG2, LOW);
    digitalWrite(DIG1, HIGH);
    if (ge == 0)  {
      pin = 5;
      for (int s = 0; s < 7; s++) {
        digitalWrite(pin, SEG7[0][s]);
        pin++;
      }
    } else if (ge == 1)  {
      pin = 5;
      for (int s = 0; s < 7; s++) {
        digitalWrite(pin, SEG7[1][s]);
        pin++;
      }
    } else if (ge == 2) {
      pin = 5;
      for (int s = 0; s < 7; s++) {
        digitalWrite(pin, SEG7[2][s]);
        pin++;
      }
    } else if (ge == 3) {
      pin = 5;
      for (int s = 0; s < 7; s++) {
        digitalWrite(pin, SEG7[3][s]);
        pin++;
      }
    } else if (ge == 4) {
      pin = 5;
      for (int s = 0; s < 7; s++) {
        digitalWrite(pin, SEG7[4][s]);
        pin++;
      }
    } else if (ge == 5) {
      pin = 5;
      for (int s = 0; s < 7; s++) {
        digitalWrite(pin, SEG7[5][s]);
        pin++;
      }
    } else if (ge == 6) {
      pin = 5;
      for (int s = 0; s < 7; s++) {
        digitalWrite(pin, SEG7[6][s]);
        pin++;
      }
    } else if (ge == 7) {
      pin = 5;
      for (int s = 0; s < 7; s++) {
        digitalWrite(pin, SEG7[7][s]);
        pin++;
      }
    } else if (ge == 8) {
      pin = 5;
      for (int s = 0; s < 7; s++) {
        digitalWrite(pin, SEG7[8][s]);
        pin++;
      }
    } else if (ge == 9) {
      pin = 5;
      for (int s = 0; s < 7; s++) {
        digitalWrite(pin, SEG7[9][s]);
        pin++;
      }
    }
    delay(10);
    //shi
    digitalWrite(DIG1, LOW);
    digitalWrite(DIG2, HIGH);
       if (shi == 0)  {
      pin = 5;
      for (int s = 0; s < 7; s++) {
        digitalWrite(pin, SEG7[0][s]);
        pin++;
      }
    } else if (shi == 1)  {
      pin = 5;
      for (int s = 0; s < 7; s++) {
        digitalWrite(pin, SEG7[1][s]);
        pin++;
      }
    } else if (shi == 2) {
      pin = 5;
      for (int s = 0; s < 7; s++) {
        digitalWrite(pin, SEG7[2][s]);
        pin++;
      }
    } else if (shi == 3) {
      pin = 5;
      for (int s = 0; s < 7; s++) {
        digitalWrite(pin, SEG7[3][s]);
        pin++;
      }
    } else if (shi == 4) {
      pin = 5;
      for (int s = 0; s < 7; s++) {
        digitalWrite(pin, SEG7[4][s]);
        pin++;
      }
    } else if (shi == 5) {
      pin = 5;
      for (int s = 0; s < 7; s++) {
        digitalWrite(pin, SEG7[5][s]);
        pin++;
      }
    } else if (shi == 6) {
      pin = 5;
      for (int s = 0; s < 7; s++) {
        digitalWrite(pin, SEG7[6][s]);
        pin++;
      }
    } else if (shi == 7) {
      pin = 5;
      for (int s = 0; s < 7; s++) {
        digitalWrite(pin, SEG7[7][s]);
        pin++;
      }
    } else if (shi == 8) {
      pin = 5;
      for (int s = 0; s < 7; s++) {
        digitalWrite(pin, SEG7[8][s]);
        pin++;
      }
    } else if (shi == 9) {
      pin = 5;
      for (int s = 0; s < 7; s++) {
        digitalWrite(pin, SEG7[9][s]);
        pin++;
      }
    }
     
  } else {
    digitalWrite(DIG1, HIGH);
    digitalWrite(DIG2, HIGH);
    for (int s = 0; s < 6; s++) {
        digitalWrite(pin, HIGH);
        pin++;
    }
    digitalWrite(pin, LOW);
  }

  //距离小于10cm时,led写入高电平
  if (distance <= 20)
  {
    //digitalWrite(led, HIGH);
    analogWrite(A1, 255);
    //tone(led,1000,1000);
    //delay(15);
    tone(led,2000,100);
    //delay(15);
    //noTone(led);
  } else {
    analogWrite(A1, 0);
    //digitalWrite(led, LOW);
  }

  delay(15);   //循环间隔60uS
}





2016-05-31 14:22:37 ling3ye 阅读数 3316
  • arduino实战

    学习如何在arduino中使用各种传感器,包括人体红外传感器,超声波传感器,舵机控制,温湿度传感器,激光接收传感器等,空气质量传感器,wifi模块等....课程内容会不断的更新增加,只要发现比较有趣的传感器就会做对应的实战视频

    405 人正在学习 去看看 陈贤能

写小小实验也一段时间了,感觉现在可以通过组合小实验,拼装一些小小作品了。

就像玩积木一样,这次做的是:


纸盒超声波测距仪


组合的功能

使用HC-SR04超声波模块,用作测量传感器;

用LCD1602 和 LCD1602 I2C模块,用作做显示数据

任意门:Arduino 和LCD1602液晶屏 I2C接口实验

任意门:Arduino 和 HC-SR04 超声波传感器 测距


大概程序思路流程就是:

读取超声波传感器数据  --  换算成CM -- 把数据显示到LCD1602上


如果觉得写得不错,又有闲余,可以支持一下

套件链接:https://item.taobao.com/item.htm?id=532904542023


实例效果

精度还是有欠缺正负3cm ,不过用于学习还是不错的



BOM表

Arduino Uno                          ×1

HC-SR04超声波传感器          ×1

LCD1602                             ×1

LCD1602 I2C 模块               ×1

9v电池座                               ×1

9V电池                                  ×1

跳线                                     若干

纸盒(二次利用,环保)      ×1


接线图



程序开源代码


#include <Wire.h>
#include <LiquidCrystal_I2C.h> //注意先添加LiquidCrystal_I2C库

#define Trig 2 
#define Echo 3 


float cm;
float temp;

LiquidCrystal_I2C lcd(0x3F,16,2);//请确定I2C模块的正确地址

void setup() {
  pinMode(Trig, OUTPUT);
  pinMode(Echo, INPUT);

  lcd.init();
  lcd.backlight();
}

void loop() {

    digitalWrite(Trig, LOW);
    delayMicroseconds(2);
    digitalWrite(Trig,HIGH); 
    delayMicroseconds(10);    
    digitalWrite(Trig, LOW); 
  
    temp = float(pulseIn(Echo, HIGH));
    cm = (temp * 17 )/1000;

  
  lcd.setCursor(0,0); 
  lcd.print("                ");
  lcd.setCursor(0,0); 
   
  lcd.print("D = ");
  lcd.print(cm);
  lcd.print(" cm");

  lcd.setCursor(0,1);  
  lcd.print("          By L.L.");

  delay(500);
}


2018-02-24 21:52:06 wfuganzhi 阅读数 890
  • arduino实战

    学习如何在arduino中使用各种传感器,包括人体红外传感器,超声波传感器,舵机控制,温湿度传感器,激光接收传感器等,空气质量传感器,wifi模块等....课程内容会不断的更新增加,只要发现比较有趣的传感器就会做对应的实战视频

    405 人正在学习 去看看 陈贤能

超声波模块为HC-SR04
    VCC->5V
    GND->GND
    TRLG->2引脚
    ECHO->9引脚

9引脚驱动 led
2019-06-16 22:12:55 LQH_weweimao 阅读数 596
  • arduino实战

    学习如何在arduino中使用各种传感器,包括人体红外传感器,超声波传感器,舵机控制,温湿度传感器,激光接收传感器等,空气质量传感器,wifi模块等....课程内容会不断的更新增加,只要发现比较有趣的传感器就会做对应的实战视频

    405 人正在学习 去看看 陈贤能

实验器材:
超声波测距仪1个
蜂鸣器1个,图示如下:
蜂鸣器
面包板、arduino板子、线

代码:
const int TrigPin = 2;
const int EchoPin = 3;
float cm;
void setup() {
Serial.begin(9600);
pinMode(TrigPin, OUTPUT);
pinMode(EchoPin, INPUT);
pinMode(8,OUTPUT);}
void loop() {
digitalWrite(8, LOW);
digitalWrite(TrigPin, LOW); //低高低电平发一个短时间脉冲去TrigPin delayMicroseconds(2);
digitalWrite(TrigPin, HIGH);
delayMicroseconds(10);
digitalWrite(TrigPin, LOW);
cm = pulseIn(EchoPin, HIGH) / 58.0; //将回波时间换算成cm
cm = (int(cm * 100.0)) / 100.0; //保留两位小数
if (cm>=2 && cm<=10)digitalWrite(8, HIGH);
}
注意事项:
使用的蜂鸣器不同,接线不一样。本实验,蜂鸣器的vcc与测距仪的vcc需用线接起来,测距仪同时接5v电源。
接线图:
在这里插入图片描述
实验心得:
物联网是个深奥的学科,自己多动手才能学的更好!!!!

超声波测距

博文 来自: TonyIOT
没有更多推荐了,返回首页