Aplicații

E-mail 1001 Solutii

Arduino Ultrasonic Sensor

The sensor sends out an ultrasonic sound wave from the TRIG pin. When the sound wave hits an object, it reflects back and is detected by the ECHO pin.
The Arduino measures the time it takes for the signal to return and uses that to calculate the distance.

Distance (cm) = (Time in microseconds × 0.034) / 2

The speed of sound is 0.034 cm/µs. We divide by 2 because the sound travels to the object and back.

Required Components

Component Quantity Purpose
Arduino Uno / Nano / Mega 1 Main microcontroller
HC-SR04 Ultrasonic Sensor 1 Measures distance
Jumper Wires 4 Connections
Breadboard (optional) 1 Easy wiring
USB Cable 1 Power & programming

Pin Connections

Connect the HC-SR04 sensor to the Arduino as shown below:

HC-SR04 Pin Arduino Pin
VCC 5V
GND GND
TRIG Digital Pin 9
ECHO Digital Pin 10

Note: The ECHO pin outputs 5V, which is safe for Arduino Uno/Nano/Mega.

Arduino Code

Upload the following code to your Arduino:


#define TRIG_PIN 9
#define ECHO_PIN 10

void setup() {
Serial.begin(9600);
pinMode(TRIG_PIN, OUTPUT);
pinMode(ECHO_PIN, INPUT);
}

void loop() {
long duration;
float distance;

// Clear trigger
digitalWrite(TRIG_PIN, LOW);
delayMicroseconds(2);

// Trigger ultrasonic burst
digitalWrite(TRIG_PIN, HIGH);
delayMicroseconds(10);
digitalWrite(TRIG_PIN, LOW);

// Measure echo time
duration = pulseIn(ECHO_PIN, HIGH);

// Convert time to distance
distance = (duration * 0.034) / 2;

// Output result
Serial.print("Distance: ");
Serial.print(distance);
Serial.println(" cm");

delay(500);
}

Code Explanation

  • pulseIn() measures the duration of the ECHO signal.
  • The sensor sends a 40 kHz ultrasonic burst using the TRIG pin.
  • Arduino calculates the distance based on the speed of sound.
  • Distance is printed in centimeters to the Serial Monitor.

Testing the Sensor

  1. Upload the code to your Arduino
  2. Open the Serial Monitor
  3. Set the baud rate to 9600
  4. Move your hand in front of the sensor
  5. Watch the distance update live

You should see something like:

Distance: 15.4 cm

Troubleshooting

Issue Cause Solution
Always reads 0 cm Incorrect pin wiring Check TRIG and ECHO
Values jump around Object surface curved or too small Use a flat object
No Serial output Wrong baud rate Set Serial Monitor to 9600
Sensor not working No power Ensure 5V and GND connected properly

 

[mai mult...]

Cum navigam fara sa pierdem timp din cauza reclamelor

Slimjet combina mai multe functii inteligente si convenabile pentru a realiza mai multe in mult mai putin timp, fara a epuiza timp pentru cautarea de pluginuri externe. De asemenea, putem adauga multe optiuni si setari pentru a personaliza aspectul si comportamentul browserului, astfel incat sa se potriveasca cel mai bine preferintelor noastre personale.

[mai mult...]

Cum ajutam la stabilitatea si buna functionare a sistemului nostru Windows

Free Window Registry Repair promite sa mentina sistemul mai stabil si sa il ajute sa functioneze mai rapid. Registrul este inima si sufletul oricarui sistem Windows. Acesta contine informatii care controleaza aspectul si comportamentul sistemului. Aproape toti utilizatorii de Windows experimenteaza treptat scaderea performantei PC-ului lor, o mare parte din aceasta poate fi atribuita erorilor Registrului Windows.

[mai mult...]

Cum eliminam fortat aplicatii defecte

Toti ne-am saturat de acele programe incapatanate care refuza sa ne paraseasca sistemul. Folosind Geek Uninstaller pentru dezinstalarea completa si fara efort a aplicatiilor. Spre deosebire de programul de dezinstalare implicit din Windows, care lasa adesea in urma ramasite de programe, Geek Uninstaller face un efort suplimentar, sapand in profunzime pentru a elimina toate fisierele si intrarile de registry ramase si partea cea mai buna este ca acesta este portabil si gratuit.

[mai mult...]

Arduino Weather Station guide

The DHT11 and DHT22 measure temperature and humidity. DHT22 offers higher accuracy and a wider range.

Pin Description
VCC +5V
DATA Data output (connect to D2)
NC Not connected
GND Ground

BMP180 / BME280

Measures pressure and temperature using the I2C interface.

Pin Description
VIN +3.3V or +5V
GND Ground
SCL I2C Clock (A5)
SDA I2C Data (A4)

LCD (16×2 I2C)

Displays data using the I2C interface — only two wires are required (SDA, SCL).
The most common I2C addresses are 0x27 or 0x3F.

Wiring Diagram

Component Arduino Pin Notes
DHT11 Data D2 Use 10kΩ pull-up resistor (optional)
BMP180 SDA A4 I2C Data
BMP180 SCL A5 I2C Clock
LCD SDA A4 Shared with BMP180
LCD SCL A5 Shared with BMP180
Power (+) 5V Common power line
Ground (–) GND Common ground

On Arduino Mega, use SDA = 20 and SCL = 21 instead.

Arduino Code Example


#include <Wire.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_BMP085.h>
#include <DHT.h>
#include <LiquidCrystal_I2C.h>

// Define sensors
#define DHTPIN 2
#define DHTTYPE DHT11   // or DHT22

DHT dht(DHTPIN, DHTTYPE);
Adafruit_BMP085 bmp;
LiquidCrystal_I2C lcd(0x27, 16, 2); // Change address if needed (0x3F or 0x27)

void setup() {
  Serial.begin(9600);
  lcd.begin(16, 2);
  lcd.backlight();
  dht.begin();

  if (!bmp.begin()) {
    Serial.println("BMP180 not found!");
    lcd.print("BMP Error!");
    while (1);
  }

  lcd.clear();
  lcd.print("Weather Station");
  delay(2000);
}

void loop() {
  float h = dht.readHumidity();
  float t = dht.readTemperature();
  float p = bmp.readPressure() / 100.0; // hPa

  if (isnan(h) || isnan(t)) {
    Serial.println("DHT read error!");
    return;
  }

  lcd.clear();
  lcd.setCursor(0, 0);
  lcd.print("T:");
  lcd.print(t);
  lcd.print((char)223);
  lcd.print("C H:");
  lcd.print(h);
  lcd.print("%");

  lcd.setCursor(0, 1);
  lcd.print("P:");
  lcd.print(p);
  lcd.print(" hPa");

  Serial.print("Temp: "); Serial.print(t);
  Serial.print(" °C  Hum: "); Serial.print(h);
  Serial.print("%  Pressure: "); Serial.print(p);
  Serial.println(" hPa");

  delay(2000);
}

Explanation: This program reads data from the DHT and BMP sensors, then prints it to both the Serial Monitor and the LCD screen.

How It Works

  1. The sensors measure temperature, humidity, and pressure.
  2. Arduino reads and formats this data.
  3. The LCD displays readings in real time.
  4. Every few seconds, the data updates automatically.

Troubleshooting

Problem Possible Cause Solution
LCD shows random text Wrong I2C address Try 0x3F instead of 0x27
“BMP180 not found!” Wiring issue Check SDA/SCL and power
DHT readings show NaN Missing library or bad sensor Install DHT library, recheck wiring
LCD blank Contrast too low or backlight off Adjust contrast screw or enable backlight

Install these from Arduino IDE → Sketch → Include Library → Manage Libraries:

  • DHT sensor library by Adafruit
  • Adafruit Unified Sensor
  • Adafruit BMP085 Library or Adafruit BME280
  • LiquidCrystal_I2C

Optional: IoT Integration

Add an ESP8266 or ESP32 to send data to cloud platforms such as:

  • ThingSpeak
  • Blynk
  • Adafruit IO

Use libraries like ESP8266WiFi.h and HTTPClient.h to upload readings as HTTP requests.

Expansion Ideas

  • Add a rain sensor or anemometer (wind speed)
  • Store data on an SD card
  • Add a real-time clock (RTC) for timestamped data
  • Display graphs on a web dashboard.
[mai mult...]

Arduino Distance Sensor with OLED Display

This project uses an HC-SR04 Ultrasonic Distance Sensor and a 0.96” I2C OLED display to measure and display the distance to an object in real-time.
It’s an excellent beginner-to-intermediate Arduino project that teaches sensor interfacing, I2C communication, and real-time data display.

In this guide, you’ll learn how to:

  • Connect and use the HC-SR04 Ultrasonic Sensor
  • Display the measured distance on an OLED screen
  • Format readings for easy readability

Components Required

Component Quantity Description
Arduino Uno / Nano / Mega 1 Main controller
HC-SR04 Ultrasonic Sensor 1 Measures distance via sound waves
0.96” OLED Display (SSD1306) 1 I2C display for output
Breadboard 1 For wiring
Jumper wires 6–8 Male-to-male connections
USB cable 1 For programming and power

The HC-SR04 measures distance by sending out an ultrasonic pulse and timing how long it takes to bounce back.
The Arduino calculates the distance based on the time delay and the known speed of sound.

Distance Formula:

Distance (cm) = (Time in microseconds × 0.034) / 2

The division by 2 accounts for the round trip of the sound wave (out and back).

Pin Connections

Component Arduino Pin Notes
HC-SR04 VCC 5V Power supply
HC-SR04 GND GND Common ground
HC-SR04 TRIG D9 Trigger pin
HC-SR04 ECHO D10 Echo pin
OLED VCC 5V Power
OLED GND GND Ground
OLED SDA A4 I2C Data
OLED SCL A5 I2C Clock

Note: On Arduino Mega, use SDA = 20 and SCL = 21 instead.

Arduino Code Example


#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>

#define TRIG_PIN 9
#define ECHO_PIN 10
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64

Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);

void setup() {
Serial.begin(9600);
pinMode(TRIG_PIN, OUTPUT);
pinMode(ECHO_PIN, INPUT);

// Initialize OLED
if (!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
Serial.println("OLED not found!");
while (true);
}

display.clearDisplay();
display.setTextSize(1);
display.setTextColor(SSD1306_WHITE);
display.setCursor(0, 0);
display.println("Distance Sensor");
display.display();
delay(1500);
}

void loop() {
long duration;
float distance;

// Send trigger pulse
digitalWrite(TRIG_PIN, LOW);
delayMicroseconds(2);
digitalWrite(TRIG_PIN, HIGH);
delayMicroseconds(10);
digitalWrite(TRIG_PIN, LOW);

// Measure echo time
duration = pulseIn(ECHO_PIN, HIGH);
distance = (duration * 0.034) / 2;

// Print to Serial Monitor
Serial.print("Distance: ");
Serial.print(distance);
Serial.println(" cm");

// Display on OLED
display.clearDisplay();
display.setTextSize(1);
display.setCursor(0, 0);
display.println("Distance Sensor");

display.setTextSize(2);
display.setCursor(0, 30);
display.print(distance, 1);
display.println(" cm");
display.display();

delay(500);
}

Explanation: The Arduino sends a pulse from the HC-SR04 and measures the time it takes to return.
It then converts this time into distance and displays it on the OLED screen.

Code Breakdown

  • pulseIn() measures the length of time the echo pin is HIGH.
  • Distance is calculated using the speed of sound (0.034 cm/μs).
  • Adafruit_SSD1306 and Adafruit_GFX libraries drive the OLED.
  • Display refreshes every 0.5 seconds for updated readings.

Install these via Arduino IDE → Sketch → Include Library → Manage Libraries:

  • Adafruit SSD1306
  • Adafruit GFX

Search for “Adafruit SSD1306” and “Adafruit GFX” and click Install.

Troubleshooting

Issue Cause Solution
OLED blank Wrong I2C address Try changing 0x3C to 0x3D
Distance always 0 Echo pin not connected Check TRIG and ECHO wiring
Unstable readings Object too close or far HC-SR04 range: 2–400 cm
Serial Monitor empty Wrong baud rate Ensure 9600 baud in Serial Monitor

Optional Upgrades

  • Add a buzzer that activates when an object is too close.
  • Display a bar graph or animation on the OLED.
  • Send readings to the cloud using ESP8266 / ESP32.
  • Log readings to an SD card.

Applications

  • Parking assist system
  • Object detection robot
  • Smart trash bin (auto open lid)
  • Liquid level detector.
[mai mult...]