Using ultrasound sensors for my project, I press a button, the ultrasound sensor reads the distance. Now, I move the sensor around and press the button, and I have noticed something strange. My max reading is 400, and when waving the sensor around, the highest reading I can get is usually around 300, and I can not read anything in between of 300 and 400. This is an HC-SR04 sensor. It makes no sense, and the code does not seem to have anything wrong in it, and as suggested in another post, i put a 10uf capacitor between the + and - pins of the sensor.
Without seeing your code and a schematic of your project it is difficult to say what is wrong, if anything
The ultrasound part is simple, just a HC-SR04 sensor connected to an ESP32 Wrover E board with logic level converters. When I press the button on the ultrasound ESP32, it sends the distance data to another ESP32 which then displays it onto a HX8357D 3.5 in LCD Display. The code is essentially: If button pressed, send distance data. I checked the code a few thousand times already, I think the code is ok, the hardware must be the problem.
Now we know that the project is more complicated that it first seems it is even more important that you post your sketches and schematics
this is the ultrasound code.
#include <esp_now.h>
#include <WiFi.h>
const int led = 2; // LED pin
const int btn = 0; // Button pin
unsigned int cmd = 0;
bool prv = 0; // Previous button state
struct Master
{
int Joystick;
bool Scan;
};
struct Slave
{
int X;
int Y;
int D;
};
Master Rcv;
Slave Send;
// REPLACE WITH THE MAC Address of your receiver
uint8_t broadcastAddress[] = {0x48, 0xE7, 0x29, 0x28, 0x9B, 0x74};
// Variable to store if sending data was successful
String success;
esp_now_peer_info_t peerInfo;
class UDS
{
private:
int ping;
int read;
int time;
int cm;
public:
// Constructor to initialize the pin
UDS(int p1, int p2)
{
ping = p1;
read = p2;
pinMode(ping, OUTPUT);
pinMode(read, INPUT);
}
// Method to turn the LED on
int CMpulse()
{
digitalWrite(ping, LOW);
delayMicroseconds(10);
digitalWrite(ping, HIGH);
delayMicroseconds(10);
digitalWrite(ping, LOW);
time = pulseIn(read, HIGH);
cm = (time * 0.017);
return cm;
}
};
UDS DS(13, 12);
// Callback when data is sent
void OnDataSent(const uint8_t *mac_addr, esp_now_send_status_t status)
{
Serial.print("\r\nLast Packet Send Status:\t");
Serial.println(status == ESP_NOW_SEND_SUCCESS ? "Delivery Success" : "Delivery Fail");
success = status == ESP_NOW_SEND_SUCCESS ? "Delivery Success :)" : "Delivery Fail :(";
}
// Callback when data is received
void OnDataRecv(const uint8_t *mac, const uint8_t *incomingData, int len)
{
if (len > 0)
{
memcpy(&Rcv, incomingData, sizeof(Rcv));
digitalWrite(led, Rcv.Scan);
Serial.print("Bytes received: ");
Serial.println(len);
}
}
void setup()
{
// Init Serial Monitor
Send.D = 15;
Serial.begin(115200);
pinMode(led, OUTPUT);
pinMode(btn, INPUT);
digitalWrite(led, LOW);
// Set device as a Wi-Fi Station
WiFi.mode(WIFI_STA);
delay(1000);
// Init ESP-NOW
if (esp_now_init() != ESP_OK)
{
Serial.println("Error initializing ESP-NOW");
return;
}
// Register send callback
esp_now_register_send_cb(OnDataSent);
// Register receive callback
esp_now_register_recv_cb(esp_now_recv_cb_t(OnDataRecv));
// Register peer
memcpy(peerInfo.peer_addr, broadcastAddress, 6);
peerInfo.channel = 0;
peerInfo.encrypt = false;
// Add peer
if (esp_now_add_peer(&peerInfo) != ESP_OK)
{
Serial.println("Failed to add peer");
return;
}
Send.X = 0;
}
void sendReadings(Slave data)
{
esp_err_t result = esp_now_send(broadcastAddress, (uint8_t *)&data, sizeof(data));
if (result == ESP_OK)
{
Serial.println("Sent with success");
}
else
{
Serial.println("Error sending the data");
}
}
void loop()
{
bool btnState = digitalRead(btn);
Send.Y = 10;
delay(100); // Debounce delay
if (prv == 0 && btnState == 1)
{
prv = btnState;
cmd = DS.CMpulse();
Serial.print(cmd);
if(cmd <= 400)
{
Send.D = cmd;
}
else
{
Send.D = 400;
}
sendReadings(Send);
if(Send.X < 480)
{
Send.X = Send.X + 10;
}
}
if (prv == 1 && btnState == 0)
{
prv = 0;
}
}
this is the screen code. ( I have it send the state of the button to make sure communication works both ways.)
#include <SPI.h>
#include <Adafruit_GFX.h>
#include <Adafruit_HX8357.h>
#include <esp_now.h>
#include <WiFi.h>
// Pin definitions
#define TFT_CS 4
#define TFT_DC 5
#define TFT_RST 18
#define TFT_MOSI 19
#define TFT_MISO 15
#define TFT_SCLK 21
bool prv = 0; // Previous button state
const int led = 2; // LED pin
const int btn = 0; // Button pin
struct Master
{
int Joystick;
bool Scan;
};
struct Slave
{
int X;
int Y;
int D;
};
Master Send;
Slave Rcv;
// REPLACE WITH THE MAC Address of your receiver
uint8_t broadcastAddress[] = {0xA0, 0xA3, 0xB3, 0x80, 0x18, 0xA4};
String success;
esp_now_peer_info_t peerInfo;
// Initialize the display
Adafruit_HX8357 tft = Adafruit_HX8357(TFT_CS, TFT_DC, TFT_RST);
void OnDataSent(const uint8_t *mac_addr, esp_now_send_status_t status)
{
Serial.print("\r\nLast Packet Send Status:\t");
Serial.println(status == ESP_NOW_SEND_SUCCESS ? "Delivery Success" : "Delivery Fail");
success = status == ESP_NOW_SEND_SUCCESS ? "Delivery Success :)" : "Delivery Fail :(";
}
void OnDataRecv(const uint8_t *mac, const uint8_t *incomingData, int len)
{
if (len > 0)
{
memcpy(&Rcv, incomingData, sizeof(Rcv));
tft.fillRect(Rcv.X, Rcv.Y, 10, 10, (tft.color565((Rcv.D * 10000 / 15686), 0, (Rcv.D * 10000 / 15686))));
tft.fillRect(Rcv.X, 20, 10, 10, (tft.color565(0, 0, 255)));
digitalWrite(led, Rcv.Y);
Serial.print("Bytes received: ");
Serial.println(len);
Serial.println(Rcv.D);
}
}
void sendReadings(Master data)
{
esp_err_t result = esp_now_send(broadcastAddress, (uint8_t *)&data, sizeof(data));
if (result == ESP_OK)
{
Serial.println("Sent with success");
}
else
{
Serial.println("Error sending the data");
}
}
void setup()
{
// Initialize serial communication
Serial.begin(115200);
pinMode(led, OUTPUT);
pinMode(btn, INPUT);
digitalWrite(led, LOW);
// Set device as a Wi-Fi Station
WiFi.mode(WIFI_STA);
delay(1000);
// Init ESP-NOW
if (esp_now_init() != ESP_OK)
{
Serial.println("Error initializing ESP-NOW");
return;
}
// Register send callback
esp_now_register_send_cb(OnDataSent);
// Register receive callback
esp_now_register_recv_cb(esp_now_recv_cb_t(OnDataRecv));
// Register peer
memcpy(peerInfo.peer_addr, broadcastAddress, 6);
peerInfo.channel = 0;
peerInfo.encrypt = false;
// Add peer
if (esp_now_add_peer(&peerInfo) != ESP_OK)
{
Serial.println("Failed to add peer");
return;
}
// Initialize the SPI bus with custom pins
SPI.begin(TFT_SCLK, TFT_MISO, TFT_MOSI, TFT_CS);
delay(1000);
Serial.println("");
Serial.println("Test Works");
// Initialize the display
tft.begin();
// Set rotation (optional)
tft.setRotation(1);
// Fill screen with black color
tft.fillScreen(0x0000); // 10x10 pixel, for 48x32 step scan. the entire screen should be made of squares of 10x10 pixels
} // 255 cm max range, for easy pixel mapping
void loop() {
bool btnState = digitalRead(btn);
delay(100); // Debounce delay
if (prv != btnState)
{
prv = btnState;
Send.Scan = btnState;
sendReadings(Send); // Send the current button state
}
}
The first thing that I would do is to write a simple sketch to read the distance and print it on the Serial monitor of a single board. Only when that worked would I add more complexity to the project
That is how I started, but it still is giving me the weird readings either way. I will continue testing.
It might just be something scattering the ultrasound in my room.
You may be expecting too much from these cheap hobby sensors. They do work well for detecting large flat surfaces that are perpendicular to the sonic beam.
They will do, barely.
Not knowing what you are doing, I cant comment one way or the other but if you think they are adequate then I'm not sure what the problem is.
This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.