Ẹkọ 71: Wọn ijinna ki o si fi han lori iboju LCD | Ẹkọ Arduino Líleṣẹ́ Ní Ìgbàkọ̀ọ̀kan
Ise agbese yii ṣe afihan bi a ṣe le darapọ̀ sensori ultrasonic HC-SR04 pẹ̀lú ifihan LCD1602 lati wọn ati ṣe afihan awọn ijinna lori Arduino. Eto yii wulo fun ọpọlọpọ awọn ohun elo ti o nilo wiwa isunmọtosi ati esi wiwo.
Awọn Ohun Elo Ti O Wulo:
- Robotics: Wiwọn awọn ijinna fun yago fun idiwọ tabi lilọ kiri.
- Iranlọwọ Iduro Ọkọ: Ṣiṣẹda sensori ijinna iduro ọkọ ti o rọrun.
- Adaṣe Ile: Kikọ eto ina ti o da lori isunmọtosi.
- Adaṣe Ile-iṣẹ: Ṣiṣakoso awọn ijinna ninu awọn ilana iṣelọpọ.
Ohun elo / Awọn ẹya
Lati kọ ise agbese yii, iwọ yoo nilo awọn ẹya wọnyi:
- Arduino Uno (tabi bọọdi ti o baamu)
- Sensori Ultrasonic HC-SR04
- Ifihan LCD 1602 (ẹya I2C ni a ṣeduro)
- Awọn okun oniho
Itọsọna Asopọmọra
A ṣe apejuwe asopọmọra ninu fidio (ninu fidio ni 00:31). Awọn asopọ pataki ni:
- Sensori Ultrasonic: VCC si 5V, GND si GND, TRIG si pin 12, ECHO si pin 11.
- LCD 1602: VCC si 5V, GND si GND, SDA si pin A4, SCL si pin A5.
%%WIRING%%
Alaye Koodu
Koodu naa lo ile-ikawe NewPing fun wiwọn ijinna ultrasonic ati ile-ikawe LiquidCrystal_I2C fun iṣakoso LCD (ninu fidio ni 02:15). Awọn paramita pataki ti o le ṣatunṣe ni:
#define TRIGGER_PIN 12 // Pin Arduino ti a so mọ pin trigger lori sensori ultrasonic.
#define ECHO_PIN 11 // Pin Arduino ti a so mọ pin echo lori sensori ultrasonic.
#define MAX_DISTANCE 200 // Ijinna ti o pọju ti a fẹ lati wọn (ni centimeters).
// Ṣeto adiresi LCD si 0x3F fun ifihan awọn ohun kikọ 16 ati ila 2
LiquidCrystal_I2C lcd(0x3F, 16, 2);
int VCC2 = 2; // VCC afikun fun LCD
Awọn TRIGGER_PIN ati ECHO_PIN n ṣalaye awọn pin Arduino ti a so mọ sensori ultrasonic. MAX_DISTANCE ṣeto ijinna ti o pọju ti a le wọn. Adiresi LCD (0x3F) le nilo atunṣe da lori iru modulu LCD rẹ. Pin VCC2 pese ipese 5V afikun fun LCD.
Awọn iṣẹ sonar.ping_cm() ati sonar.ping_in() ni a lo lati gba awọn wiwọn ijinna ni centimeters ati inches ni atele (ninu fidio ni 05:27 ati 06:01).
Ise Agbese Laaye / Ifihan
A ṣe afihan ise agbese yii ninu fidio (ninu fidio ni 06:42). Fidio naa ṣe afihan sensori ti n wọn awọn ijinna si ọpọlọpọ awọn nkan ati ṣe afihan awọn abajade ni centimeters ati inches lori iboju LCD. Atẹle oni-nọmba ṣe afihan awọn wiwọn kanna, ti o pese ọna lati rii daju awọn kika naa.
Awọn ipin
- [00:04] Ifihan ati Akopọ Ise Agbese
- [00:31] Awoṣe Asopọmọra ati Awọn Asopọ
- [02:15] Alaye Koodu: Ṣeto Sensori Ultrasonic
- [03:19] Alaye Koodu: Ṣeto LCD1602
- [04:21] Alaye Koodu: Ṣeto Arduino
- [04:46] Alaye Koodu: Akọkọ Lupu ati Ifihan
- [06:42] Ifihan Laaye
- [07:26] Ipari
/*
* S08-03
* Using LCD1602-I2C and HC-SR04 Ultrasonic Sensor with Arduino
* Written/updated by Ahmad Shamshiri for Robojax (Robojax.com)
* on January 16, 2019 at 15:51 in Ajax, Ontario, Canada
* This code is "AS IS" without warranty or liability. Free to be used as long as you keep this note intact.*
* This code has been download from Robojax.com
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#include <NewPing.h>
#define TRIGGER_PIN 12 // Arduino pin tied to trigger pin on the ultrasonic sensor.
#define ECHO_PIN 11 // Arduino pin tied to echo pin on the ultrasonic sensor.
#define MAX_DISTANCE 200 // Maximum distance we want to ping for (in centimeters). Maximum sensor distance is rated at 400-500cm.
NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE); // NewPing setup of pins and maximum distance.
// start of settings for LCD1602-I2C
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
// Set the LCD address to 0x3F for a 16 chars and 2 line display
LiquidCrystal_I2C lcd(0x3F, 16, 2);
int VCC2 = 2;
// end of settings for LCD1602-I2C
void setup() {
Serial.begin(9600); // Open serial monitor at 19600 baud to see ping results.
pinMode(VCC2, OUTPUT);// extra VCC for LCD
digitalWrite(VCC2, HIGH);//extra VCC is now HIGH (5V)
// initialize the LCD,
lcd.begin();
// Turn on the blacklight and print a message.
lcd.backlight();
lcd.clear();
lcd.setCursor (0,0); //
lcd.print("Robojax LCD1602");
}
void loop() {
delay(50); // Wait 50ms between pings (about 20 pings/sec). 29ms should be the shortest delay between pings.
lcd.clear();// clearn previous values from screen
lcd.setCursor (0,0); //character zero, line 1
lcd.print("LCD1602 HC-SR04"); // print text
// print distance in cm
lcd.setCursor (0,1); //character 0, line 2
lcd.print(sonar.ping_cm());// print distance in cm
lcd.setCursor (3,1); //character 4, line 2
lcd.print("cm");// print "cm" on the display
// print distance in inch
lcd.setCursor (7,1); //character 8, line 2
lcd.print(sonar.ping_in());// print distance in cm
lcd.setCursor (9,1); //character 4, line 2
lcd.print("inches");// print "cm" on the display
Serial.print("Ping: ");
Serial.print(sonar.ping_cm()); // Send ping, get distance in cm and print result (0 = outside set distance range)
Serial.print("cm");
Serial.print(", ");
Serial.print(sonar.ping_in());
Serial.println("in");
int distance = sonar.ping_cm();
}
Ohun ti o le nilo
-
eBay
-
eBayPurchase LCD1602-I2C from eBayebay.us
-
AliExpressPurchase LCD1602-I2C from AliExpresss.click.aliexpress.com
-
BanggoodPurchase HC-SR04 Ultrasonic Sensor from Banggoodbanggood.com
Orisun & itọkasi
Ko si awọn orisun sibẹsibẹ.
Fáìlìs📁
Ko si awọn faili ti o wa.