Blog

Blog

PHODAL

arduino 温度传感器与超声波传感器

arduino lm35

LM35是NS公司生产的集成电路温度传感器系列产品之一,它具有很高的工作精度和较宽的线性工作范围,该器件输出电压与摄氏温度线性成比例。因而,从使用角度来说, LM35与用开尔文标准的线性温度传感器相比更有优越之处,LM35无需外部校准或微调,可以提供±1/4℃的常用的室温精度。


float temp;
int tempPin = 0;

void setup() { Serial.begin(9600); }

void loop() { temp = analogRead(tempPin); temp = temp * 0.48828125; Serial.print("TEMPRATURE = "); Serial.print(temp); Serial.print("*C"); Serial.println(); delay(1000); }

arduino hc-sr04

HC-SR04 超声波测距模块可提供2cm-400cm 的非接触式距离感测功能,. 测距精度可达高到3mm;模块包括超声波发射器、接收器与控制电路。


const int trigPin = 2;
const int echoPin = 4;

void setup() { // initialize serial communication: Serial.begin(9600); }

void loop() { // establish variables for duration of the ping, // and the distance result in inches and centimeters: long duration, inches, cm;

// The sensor is triggered by a HIGH pulse of 10 or more microseconds. // Give a short LOW pulse beforehand to ensure a clean HIGH pulse: pinMode(trigPin, OUTPUT); digitalWrite(trigPin, LOW); delayMicroseconds(2); digitalWrite(trigPin, HIGH); delayMicroseconds(10); digitalWrite(trigPin, LOW);

// Read the signal from the sensor: a HIGH pulse whose // duration is the time (in microseconds) from the sending // of the ping to the reception of its echo off of an object. pinMode(echoPin, INPUT); duration = pulseIn(echoPin, HIGH);

// convert the time into a distance inches = microsecondsToInches(duration); cm = microsecondsToCentimeters(duration);

Serial.print(inches); Serial.print("in, "); Serial.print(cm); Serial.print("cm"); Serial.println();

delay(100); }

long microsecondsToInches(long microseconds) { // According to Parallax's datasheet for the PING))), there are // 73.746 microseconds per inch (i.e. sound travels at 1130 feet per // second). This gives the distance travelled by the ping, outbound // and return, so we divide by 2 to get the distance of the obstacle. // See: http://www.parallax.com/dl/docs/prod/acc/28015-PING-v1.3.pdf return microseconds / 74 / 2; }

long microsecondsToCentimeters(long microseconds) { // The speed of sound is 340 m/s or 29 microseconds per centimeter. // The ping travels out and back, so to find the distance of the // object we take half of the distance travelled. return microseconds / 29 / 2; }

结合

最后代码:


const int trigPin = 13;
const int echoPin = 12;

float temp; int tempPin = 0;

void setup() { Serial.begin(9600); }

void loop() { long duration, inches, cm;

pinMode(trigPin, OUTPUT); digitalWrite(trigPin, LOW); delayMicroseconds(2); digitalWrite(trigPin, HIGH); delayMicroseconds(10); digitalWrite(trigPin, LOW);

pinMode(echoPin, INPUT); duration = pulseIn(echoPin, HIGH);

cm = microsecondsToCentimeters(duration);

Serial.print(cm); Serial.print("cm"); Serial.println(); temp = analogRead(tempPin); temp = temp * 0.48828125; Serial.println(temp); delay(1000);
}

long microsecondsToInches(long microseconds) { return microseconds / 74 / 2; }

long microsecondsToCentimeters(long microseconds) { return microseconds / 29 / 2; }


或许您还需要下面的文章:

关于我

Github: @phodal     微博:@phodal     知乎:@phodal    

微信公众号(Phodal)

围观我的Github Idea墙, 也许,你会遇到心仪的项目

QQ技术交流群: 321689806
comment

Feeds

RSS / Atom

最近文章

关于作者

Phodal Huang

Engineer, Consultant, Writer, Designer

ThoughtWorks 技术专家

工程师 / 咨询师 / 作家 / 设计学徒

开源深度爱好者

出版有《前端架构:从入门到微前端》、《自己动手设计物联网》、《全栈应用开发:精益实践》

联系我: h@phodal.com

微信公众号: 最新技术分享

标签