Displaying and sending data(HCSR 04 ultrasonic sensor) from arduino uno on serial monitor in arduino IDE via ESP8266 nodemcu v1

We are trying to make an intrusion detection system using ultrasonic sensor, arduino UNO and nodemcu v1. The basic idea is to connect ultrasonic sensor to arduino(because the sensors output voltage is 0-5 v and esp's ADC doesnt support it ) and send it's readings to a platform like blynk via ESP8266. For now we have just tried serial communication (UART) between arduino uno and esp and display the results on serial monitor. However, while the sensor is working fine, the readings are not displayed on the serial monitor of esp code. What to do? We have to show in 3 days.

This is code for arduino and ultrasonic sensor:

#include <SoftwareSerial.h>

#define TRIG_PIN 9 // Trigger pin for HC-SR04
#define ECHO_PIN 8 // Echo pin for HC-SR04

SoftwareSerial mySerial(2, 3); // RX, TX (for communication with ESP8266)

void setup() {
Serial.begin(9600); // Serial monitor for debugging
mySerial.begin(115200); // Communication with ESP8266
pinMode(TRIG_PIN, OUTPUT);
pinMode(ECHO_PIN, INPUT);
}

void loop() {
// Trigger ultrasonic pulse
digitalWrite(TRIG_PIN, LOW);
delayMicroseconds(2);
digitalWrite(TRIG_PIN, HIGH);
delayMicroseconds(10);
digitalWrite(TRIG_PIN, LOW);

// Measure echo pulse duration
long duration = pulseIn(ECHO_PIN, HIGH);
float distance = duration * 0.034 / 2; // Convert to cm

// Send data to ESP8266
mySerial.print(distance);
mySerial.println(" cm");

Serial.print("Sent: "); // Debugging message
Serial.print(distance);
Serial.println(" cm");

delay(1000); // 1-second delay between readings

}
Here serial monitor baud rate is 9600

This is code for esp:

void setup() {
Serial.begin(115200); // Start Serial Monitor for debugging
}

void loop() {
if (Serial.available()) {
String data = Serial.readStringUntil('\n'); // Read data from Arduino
Serial.print("Received Distance: ");
Serial.println(data);
}
}
Here baud rate for serial monitor is 115200.

Welcome to the forum

Please follow the advice given in the link below when posting code, in particular the section entitled 'Posting code and common code problems'

Use code tags (the < CODE/ > icon above the compose window) to make it easier to read and copy for examination

https://forum.arduino.cc/t/how-to-get-the-best-out-of-this-forum

Please post your full sketch, using code tags when you do

Posting your code using code tags prevents parts of it being interpreted as HTML coding and makes it easier to copy for examination

In my experience the easiest way to tidy up the code and add the code tags is as follows

Start by tidying up your code by using Tools/Auto Format in the IDE to make it easier to read. Then use Edit/Copy for Forum and paste what was copied in a new reply. Code tags will have been added to the code to make it easy to read in the forum thus making it easier to provide help.

It is also helpful to post error messages in code tags as it makes it easier to scroll through them and copy them for examination

I'm pretty sure it would be easier to use level shifter or voltage divider to drop the sensor output to 3.3V than marrying Arduino and Esp.
And if you go with your idea, you need level shifter for the serial line between Uno and Esp anyway... So what's the point?

Also, you don't connect that sensor to ADC.

2 Likes

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.