环境搭建:
1. Arduino UNO R3开发板,
![Arduino开发之SEN0132 Carbon Monoxide Sensor (MQ7) Arduino开发之SEN0132 Carbon Monoxide Sensor (MQ7)](https://image.shishitao.com:8440/aHR0cHM6Ly9waWFuc2hlbi5jb20vaW1hZ2VzLzUxOC9jMmI3NTBlN2Y0Y2E4NmM1NWMyNmIxMTRlMzNjZjIxNi5wbmc%3D.png?w=700&webp=1)
![Arduino开发之SEN0132 Carbon Monoxide Sensor (MQ7) Arduino开发之SEN0132 Carbon Monoxide Sensor (MQ7)](https://image.shishitao.com:8440/aHR0cHM6Ly9waWFuc2hlbi5jb20vaW1hZ2VzLzI0LzI4ZjQ0ZDQ3MWQ0ZGZmYjAyNjFmNDRlZmNiMTM1MWQ4LnBuZw%3D%3D.png?w=700&webp=1)
2. Arduino IDE。
我这里使用的是1.8.5。可以在https://www.arduino.cc/en/Main/Software下载并安装。
安装好之后,桌面会有如下图标。
![Arduino开发之SEN0132 Carbon Monoxide Sensor (MQ7) Arduino开发之SEN0132 Carbon Monoxide Sensor (MQ7)](https://image.shishitao.com:8440/aHR0cHM6Ly9waWFuc2hlbi5jb20vaW1hZ2VzLzM2NS84MGE1MDE2ZmZkYWE3MTU2Y2Q5MTI2MDc1YTg5NWIyZC5wbmc%3D.png?w=700&webp=1)
示例开发:
1.连接设备。
本例中我们以SEN0132 Carbon Monoxide Sensor(MQ7)并结合DFR0021-R为例,基于Arduino Uno R3和Arduino IDE开发。
DFR0021-R的引脚和Arduino Uno开发板的连接方式如下,
DFR0021-R |
SEN0132 |
Arduino Uno R3 |
VCC |
VCC |
3.3V |
GND |
GND |
GND |
信号引脚 |
信号引脚 |
DFR0021接数字引脚8,SEN0132
接模拟引脚A0
|
2. 编码。
连接好之后,用数据线连接Arduino开发板和电脑。同时打开Arduino IDE。输入下述代码。
#define inPin A0
#define ledPin 8
#define GAS_RANGE 660
void setup()
{
Serial.begin(9600);
pinMode(ledPin, OUTPUT); // declare LED as output
pinMode(inPin, INPUT); // declare Carbon Monoxide sensor as input
}
void loop()
{
Serial.print("Reading Gas Sensor (MQ7) Value: \n");
uint16_t value = analogRead (inPin);
Serial.println(value, DEC);
Serial.println(" \n");
if (value > GAS_RANGE) // check if the gas value is larger than defined range
{
digitalWrite(ledPin, HIGH); // turn LED ON
Serial.print("LED is On...\n");
}
else
{
digitalWrite(ledPin, LOW); // turn LED OFF
Serial.print("LED is Off...\n");
}
Serial.println();
delay (1000);
}
然后保存文件。
![Arduino开发之SEN0132 Carbon Monoxide Sensor (MQ7) Arduino开发之SEN0132 Carbon Monoxide Sensor (MQ7)](https://image.shishitao.com:8440/aHR0cHM6Ly9waWFuc2hlbi5jb20vaW1hZ2VzLzEwOC8wM2U4MWFlM2JkMWViMjhkNzU5ZDY5MjU4ZDhhOTI0Yy5wbmc%3D.png?w=700&webp=1)
选择Arduino Uno开发板。
![Arduino开发之SEN0132 Carbon Monoxide Sensor (MQ7) Arduino开发之SEN0132 Carbon Monoxide Sensor (MQ7)](https://image.shishitao.com:8440/aHR0cHM6Ly9waWFuc2hlbi5jb20vaW1hZ2VzLzI3OS9mOTY4MDgwYTZiNDc1YWFlMzEwM2VkMmEyZjMxNjM4Ny5wbmc%3D.png?w=700&webp=1)
编译上传大到开发板。
![Arduino开发之SEN0132 Carbon Monoxide Sensor (MQ7) Arduino开发之SEN0132 Carbon Monoxide Sensor (MQ7)](https://image.shishitao.com:8440/aHR0cHM6Ly9waWFuc2hlbi5jb20vaW1hZ2VzLzc5MC82Mzg5NGUzNDE5ZDUyNmM0MDUzNGJiMjBmZTNhMjhmZS5wbmc%3D.png?w=700&webp=1)
3.运行。
选择COM口信息,
![Arduino开发之SEN0132 Carbon Monoxide Sensor (MQ7) Arduino开发之SEN0132 Carbon Monoxide Sensor (MQ7)](https://image.shishitao.com:8440/aHR0cHM6Ly9waWFuc2hlbi5jb20vaW1hZ2VzLzQ3My8yMjRkY2NlMjE0MWMzNWNhOTk1N2Y4YTg0M2YwZDQ1OS5wbmc%3D.png?w=700&webp=1)
然后选择端口监视工具,查看程序运行信息。
![Arduino开发之SEN0132 Carbon Monoxide Sensor (MQ7) Arduino开发之SEN0132 Carbon Monoxide Sensor (MQ7)](https://image.shishitao.com:8440/aHR0cHM6Ly9waWFuc2hlbi5jb20vaW1hZ2VzLzc0LzNmMjI3Y2EzMmU4NWIxNzg1NmIwYTgxYTVhZmJjMjEyLnBuZw%3D%3D.png?w=700&webp=1)
串口监视信息,
![Arduino开发之SEN0132 Carbon Monoxide Sensor (MQ7) Arduino开发之SEN0132 Carbon Monoxide Sensor (MQ7)](https://image.shishitao.com:8440/aHR0cHM6Ly9waWFuc2hlbi5jb20vaW1hZ2VzLzczL2E4MWY2N2NhZGUwNTVmYzQ4NjQ5MmMyNDVlMzQ2ZWMxLnBuZw%3D%3D.png?w=700&webp=1)
备注:上面的数据就是当前Gas Sensor的值和Red LED 的当前状态(ON表示开,OFF表示关)。此例中,当测到的值大于660时,LED灯就会亮。
整体运行界面:
实际效果图:
![Arduino开发之SEN0132 Carbon Monoxide Sensor (MQ7) Arduino开发之SEN0132 Carbon Monoxide Sensor (MQ7)](https://image.shishitao.com:8440/aHR0cHM6Ly9waWFuc2hlbi5jb20vaW1hZ2VzLzE1My9jMDI3NzMzM2NhNDU0MGJlYzBkNTU4OGZiNmQ0YzY3MS5wbmc%3D.png?w=700&webp=1)