MQ-3 Mạch Cảm Biến Độ Cồn Ethanol được trang bị LED báo trạng thái ngõ ra, đầu ra tín hiệu kép (Analog và Digital TTL). TTL có thể được kết nối trực tiếp với vi điều khiển. Điện áp đầu ra tương tự 0-5V, nồng độ càng cao thì điện áp ra càng cao.
Mạch cảm biến nhạy với Ethanol, tốc độ đáp ứng nhanh, tuổi thọ cao, được ứng dụng trong nhiều lĩnh vực như: do nồng độ cồn, kiểm tra người uống rượu lái xe,...
Thông số kỹ thuật:
- Kích thước: 32x22x22mm
- Chip chính: LM393
- Điện áp làm việc: 3~5 VDC
- Dải đo: 10 ~ 1000ppm
Phần mềm:
Phần cứng cần thiết:
Sơ đồ kết nối:
Thư viện:
Cài đặt thư viện:
- Cài thư viện Adafruit_SSD1306-master.zip và Adafruit-GFX-Library-master.zip
- Trong Arduino IDE, vào "Sketch" -> "Include Library" -> "Add .ZIP Libraries..."
Code scan địa chỉ i2c
// --------------------------------------
// i2c_scanner
// --------------------------------------
#include <Wire.h>
void setup() {
Wire.begin();
Serial.begin(9600);
while (!Serial); // Leonardo: wait for serial monitor
Serial.println("\nI2C Scanner");
}
void loop() {
int nDevices = 0;
Serial.println("Scanning...");
for (byte address = 1; address < 127; ++address) {
// The i2c_scanner uses the return value of
// the Write.endTransmisstion to see if
// a device did acknowledge to the address.
Wire.beginTransmission(address);
byte error = Wire.endTransmission();
if (error == 0) {
Serial.print("I2C device found at address 0x");
if (address < 16) {
Serial.print("0");
}
Serial.print(address, HEX);
Serial.println(" !");
++nDevices;
} else if (error == 4) {
Serial.print("Unknown error at address 0x");
if (address < 16) {
Serial.print("0");
}
Serial.println(address, HEX);
}
}
if (nDevices == 0) {
Serial.println("No I2C devices found\n");
} else {
Serial.println("done\n");
}
delay(5000); // Wait 5 seconds for next scan
}
Sau khi nạp code xong các bạn có thể mở Serial monitor lên để xem địa chỉ i2c của LCD rồi thay vào code MQ-3. Ở dây LCD của mình có địa chỉ là 0x27 nên mình sẽ thay là:
void setup() {
display.begin(SSD1306_SWITCHCAPVCC, 0x27); //thay địa chỉ i2c ở đây
display.clearDisplay();
Serial.begin(9600);
}
Code MQ-3 với Arduino:
#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#define OLED_RESET 4
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
int TIME_UNTIL_WARMUP = 100;
unsigned long time;
int analogPin = 0;
int val = 0;
void setup() {
display.begin(SSD1306_SWITCHCAPVCC, 0x27); //thay địa chỉ i2c ở đây
display.clearDisplay();
Serial.begin(9600);
}
void loop() {
delay(100);
val = readAlcohol();
printTieuDe();
printLamNong();
time = millis()/1000;
if(time<=TIME_UNTIL_WARMUP)
{
time = map(time, 0, TIME_UNTIL_WARMUP, 0, 100);
display.drawRect(10, 50, 110, 10, WHITE); //Empty Bar
display.fillRect(10, 50, time,10,WHITE);
}else
{
printTieuDe();
printNongDoCon(val);
printThongBao(val);
}
display.display();
}
void printTieuDe()
{
display.clearDisplay();
display.setTextSize(1);
display.setTextColor(WHITE);
display.setCursor(10,0);
display.println("May Do Nong Do Con");
}
void printLamNong()
{
display.setTextSize(1);
display.setTextColor(WHITE);
display.setCursor(15,15);
display.println("Vui long cho...");
display.setCursor(5,30);
display.println("Dang lam nong sensor");
}
void printNongDoCon(int value)
{
display.setTextSize(2);
display.setTextColor(WHITE);
display.setCursor(45,25);
display.println(val);
}
void printThongBao(int value)
{
display.setTextSize(1);
display.setTextColor(WHITE);
display.setCursor(0,55);
if(value<200)
{
display.println("Ban dang tinh tao");
Serial.println("Ban dang tinh tao");
}
if (value>=200 && value<280)
{
display.println("Ban da uong bia,ruou");
Serial.println("Ban da uong bia,ruou");
}
if (value>=280 && value<350)
{
display.println("Ban da uong bia,ruou");
Serial.println("Ban da uong bia,ruou");
}
if (value>=350 && value <500)
{
display.println("Ban da uong bia,ruou");
Serial.println("Ban da uong bia,ruou");
}
if(value>500)
{
display.setCursor(30,55);
display.print("Ban da say!");
Serial.println("Ban da say!");
}
}
int readAlcohol()
{
int val = 0;
int val1;
int val2;
int val3;
display.clearDisplay();
val1 = analogRead(analogPin);
delay(10);
val2 = analogRead(analogPin);
delay(10);
val3 = analogRead(analogPin);
val = (val1+val2+val3)/3;
return val;
}
Nhận xét