I'm trying to convert the following sketch which is for an Arduino Uno board to an ESP32 board and pins.
Are there pins on the ESP32 that the SoftwareSerial library would work with, or is there another approach that I need to take to read from A02YYUW sensor on an ESP32 board? Any guidance would be appreciated!
Thanks in advance,
Jordan
/*
*@File : DFRobot_Distance_A02.ino
*@Brief : This example use A02YYUW ultrasonic sensor to measure distance
* With initialization completed, We can get distance value
*@Copyright [DFRobot](https://www.dfrobot.com),2016
* GUN Lesser General Pulic License
*@version V1.0
*@data 2019-8-28
*/
#include <SoftwareSerial.h>
SoftwareSerial mySerial(11,10); // RX, TX
unsigned char data[4]={};
float distance;
void setup()
{
Serial.begin(57600);
mySerial.begin(9600);
}
void loop()
{
do{
for(int i=0;i<4;i++)
{
data[i]=mySerial.read();
}
}while(mySerial.read()==0xff);
mySerial.flush();
if(data[0]==0xff)
{
int sum;
sum=(data[0]+data[1]+data[2])&0x00FF;
if(sum==data[3])
{
distance=(data[1]<<8)+data[2];
if(distance>30)
{
Serial.print("distance=");
Serial.print(distance/10);
Serial.println("cm");
}else
{
Serial.println("Below the lower limit");
}
}else Serial.println("ERROR");
}
delay(100);
}
Watch out for pin numbers when changing controller. Some use names like GPIOnn etc. Then You need to know if that GPIO pin provides the functionality You want. Remember to set up the IDE for the new controller.
see post ESP32 to ESP-CAM for Serial1 code for an ESP32
you should be able to connect it to a UNO using SoftwareSerial on pins 2 and 3
make sure you reduce the baudrate as SoftwareSerial will not work over 38400baud on a UNO
remember the UNO uses 5V logic and the ESP32 3.3V logic - you require a potential divider on the UNO Tx to ESP32 Rx or you can damage the ESP
any particular reason to use a UNO and ESP32 - could you not just use the ESP32?
/* There are three serial ports on the ESP known as U0UXD, U1UXD and U2UXD.
* U0UXD is used to communicate with the ESP32 for programming and during reset/boot.
* U1UXD is unused and can be used for your projects. Some boards use this port for SPI Flash access though
* U2UXD is unused and can be used for your projects.
*/
There are 3 options on the ES32 - but the wrover has limited exposed options -
UART0
UART1
UART2
UART0 - same as USB port - I need this one for Serial communications
UART1 - GPIO9, GPIO10 - see below code
UART2 - pins are unavailable - are marked as GND
WiFi should have worked - what test did you do? did it compile? did it load? what did the Serial Monitor display?
probably the first and simplest test is to scan for nearby WiFi networks as suggested in getting-started-with-esp32
I have a number of ESP32 boards but mainly use the NodeMCU ESP32 with onboard WiFi, Bluetooth classic 2.0 and BLE - also hardware serial ports Serial1 and Serial2 work OK
Part of the problem was the example code that DFRobot provided on this page
was somehow never able to read successfully so I assumed the Serial port was not up.
I rewrote the serial reader essentially to get it working consistently. Not sure if anyone else has had a similar problem but if you are interested let me know.
Hiya Mate. Attempted to DM you but couldn't find such a function. I was hoping you could provide me some assistance with a similar matter. I'm stumped on how to interface with the A02YYUW sensor (with an ESP32) given i need to use the Software Serial function. I simply cannot get an accurate readout from the sensor. Any advice/ insight?
All im trying to do at the moment is get a simple print-out of the distance reading, as the sample script does.
Jordan,
Thank you for your response! Unfortunately, while it didn't solve my issues, I do believe your code has helped point me in the right direction. In an effort to not de-rail your thread further, I will be creating a new thread with my issues. Figers crossed I can solve them! Thanks again.