原创博文,转载请注明出处
前言
作为一名嵌入式系统的初学者,我常常用手中的板子做一些能够让生活更加简便的小应用。但是往往在一段时间后,当我想对这些小应用做些升级的时候,总是由于之前开发的过程中没有做过系统的记录,对整个系统的升级过程是十分困难的。甚至个别复杂的应用我直接放弃阅读冗长的代码和分析杂乱的焊接电路,直接进行重新开发。
为了改变这个情况,我想在这里对我做过的小应用进行系统的记录。方便自己以后对应用的升级,也希望对其他的嵌入式爱好者有所帮助。同时如果各位朋友发现我的错误,我也乐于接受大家对我的指摘。
准备
Arduino Nano 开发板 *1(其他系列的Arduino开发板也可共享我的代码)
按钮 *1
1.5kΩ 电阻 *1
510Ω 电阻 *1
DS18B20 温度传感器 *1
LCD1602 液晶屏 *1
电线 若干
以下器件如果您要在面包板上做,可省略:
电工工具
公口排针15pin *2
公口排针3pin *1
母口排针16pin *1
PCB版 5*7cm *1
亚克力板(做保护膜用的,可省略)
电路图
面包板线路图
代码
// for thermometer
#include
#include
// for LCD
#include
#define ONE_WIRE_BUS 6
// Set up a oneWire instance to communicate with any
OneWire device
OneWire ourWire(ONE_WIRE_BUS);
// Tell Dallas Temperature Library to use oneWire
Library
DallasTemperature sensors(&ourWire);
const int backlight = 8; // pin for backlight of LCD
screen
// initialize the library with the numbers of the
interface pins
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
int outputState = HIGH; //
variable for reading the output status
const int button_in = 9;
int buttonState = 0;
int preState = 0;
void setup()
{
// Serial port
// Start serial port to see
results
Serial.begin(9600);
Serial.println("Thermometer with LCD
Test Program");
Serial.println("Temperature Sensor
DS18B20");
// Themometer
// Start up the DallasTemperature
library
sensors.begin();
// LCD
// Set up the LCD's number of columns
and rows:
lcd.begin(16, 2);
// Print a message to the
LCD.
lcd.print("Temperature");
// PIN
pinMode(backlight,OUTPUT);
pinMode(button_in,INPUT);
}
void loop()
{
//Button
// read the state of the pushbutton
value:
preState = buttonState;
buttonState =
digitalRead(button_in);
// check if the pushbutton
is pressed.
// if it is, the
buttonState is HIGH:
if (buttonState == HIGH) {
//
while(buttonState == HIGH)buttonState =
digitalRead(button_in);
if (preState !=
HIGH)
outputState =
!outputState;
}
digitalWrite(backlight, outputState); // turn the LED on (HIGH is the
voltage level)
//Thermometer
Serial.println();
Serial.print("Requesting
temperature...");
sensors.requestTemperatures();
// Send the command to get
temperatures
Serial.println("DONE");
Serial.print(sensors.getTempCByIndex(0));
Serial.println(" Degrees
C");
//LCD
// set the cursor to column 0, line
1
// (note: line 1 is the
second row, since counting begins with 0):
lcd.setCursor(0, 1);
// print the number of
seconds since reset:
lcd.print(sensors.getTempCByIndex(0));
lcd.setCursor(5, 1);
lcd.print("C");
delay(100);
}
焊接与组装
焊接
我也是刚刚开始学习焊接,焊接的水平十分有限,成品的外观不怎么美观,但是功能还是没问题的。
组装