风筝
发表于: 2018-8-11 17:10:34 | 显示全部楼层

实际上,与Arduino开发板进行串口通信非常简单。在本篇文章中,我们将介绍如何进行温度测量,然后通过串口发送测量结果。


与其他设备不同,Arduino串口通信非常易于使用。串口通信可以是有线或无线的,例如串口蓝牙连接。 Arduino编程环境有一个串口监视器的工具,专门用于查看串口数据通信。要激活通信,使用以下方法:

1.    转到工具栏;

2.   单击串口监视器(Serial Monitor)菜单;

3.   选择Serial.begin()函数设定的波特率。


以下是一些Arduino串口通信函数。


Serial.begin(speed)

此函数设置串行通信速度。它有一个参数speed,通常设置为9600。


Serial.read()

此函数从串口接收数据。


Serial.write(VAL)

该函数通过串口发送数据。参数val可以是单个变量、字符串或数组。


Serial.printIn(val,format)

此函数使用某种特定格式将val打印到Arduino IDE串口监视器。


使用LM35进行温度测量

LM35是一款用于测量环境温度的温度传感器。该设备的不同封装如下所示。

serial_1.png

LM35提供与温度成比例的线性输出,0 V对应0℃,每度C变化输出电压变化10 mV。 LM35比热敏电阻和热电偶更容易使用,因为它们是线性的,不需要信号调制。


LM35的输出可以直接连接到Arduino模拟输入。由于Arduino模数转换器(ADC)的分辨率为1024位,参考电压为5 V,因此用于根据ADC值计算温度的公式为:

  1. temp = ((5.0 * analogRead(pin)) / 1024) * 100.0
复制代码

实验所需的硬件

●    LM35温度传感器

●    LED指示灯

●    220欧姆电阻器

●    Arduino Mega2560开发板

●    面包板

●    连接导线


电路原理图

comm-serially-arduino.jpg


按照上面的电路图所示连接组件。传感器由Arduino的5V和GND引脚供电。其输出端子连接到Arduino的引脚A0。当使用函数analogRead(0)读取该模拟电压的值时,程序返回0到1023之间的值。程序使用以下公式计算温度:

  1. temp = ((5.0 * analogRead(pin)) / 1024) * 100.0
复制代码

或者

  1. temp =  analogRead(pin) * 0.48828125;
复制代码

最后,温度读数使用Serial.print()函数打印输出到IDE串口监视器。如下面的截图所示:

serial_3.png


代码

该实验的代码如下所示。除了在IDE串口监视器上显示温度之外,如果测量温度约为70摄氏度,程序将点亮黄色LED指示灯,如果低于70摄氏度,程序将点亮绿色LED指示灯。通过使用if-else条件语句和digitalWrite(pin,status)实现。

  1. const int adc = 0 ;                 //naming pin 0 of analog input side as adc
  2. const int high = 8 ;                 // For turning on and off yellow LED
  3. const int low  = 9 ;                 // For turning on and off Green LED

  4. void setup()
  5. {
  6. Serial.begin(9600) ;                  //Starting serial Communication at baud rate of 9600
  7.                                                       
  8. pinMode(high,OUTPUT);         //declaring LED pins as OUTPUT
  9. pinMode(low,OUTPUT);
  10. }

  11. void loop()
  12. {

  13.   int adc  = analogRead(0) ;                     //reading analog voltage and storing it in an integer
  14.   adc = adc * 0.48828125;                        //converting reading into Celsius
  15.   Serial.print("TEMPRATURE = ");         //to Display on serial monitor  
  16.   Serial.print(adc);                            //this will show the actualtemp
  17.   Serial.print("*C");                           //TEMPRATURE = 27*C ETC
  18.   Serial.println();                                      //To end the line  
  19.   delay(1000);                                   //1 Sec delay
  20.   
  21.   /*
  22.   if (temperature (adc) > 70 ° C )
  23.           turn on Yellow Leds
  24.           turn off Green Leds
  25.   else
  26.         turn off Yellow Leds
  27.         turn on Green Led
  28.   */
  29.   
  30.   if(adc>70)                                  // This is the control statement
  31.   {
  32.     digitalWrite(high,HIGH) ;
  33.     digitalWrite(low,LOW) ;
  34.   }
  35.    else
  36.    {
  37.     digitalWrite(high,LOW) ;
  38.     digitalWrite(low,HIGH) ;
  39.    }
  40. }
复制代码

跳转到指定楼层
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

主题 700 | 回复: 1481



手机版|

GMT+8, 2024-4-28 04:13 , Processed in 0.042723 second(s), 6 queries , Gzip On, MemCache On. Powered by Discuz! X3.5

YiBoard一板网 © 2015-2022 地址:河北省石家庄市长安区高营大街 ( 冀ICP备18020117号 )

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