I have been tinkering like mad but can’t seem to find the answer to what I thought would be a simple sensor hookup. I am trying to use a A02YYUW Waterproof Sonic sensor with a Boron (Waterproof Ultrasonic Distance Sensors with Arduino). The problem is the device (3.3v) uses serial UART and I can’t seem to get Serial1 to work assuming Serial1 connects through RX and TX pins.
I have connected the sensors VCC, GRND, RX, TX to the Boron’s pins but the Serial connection doesn’t seem to be made. I have searched the community board but not found anything that has helped.
This is a version of the original script:
/*
JSN-SR04T-V3.0 Ultrasonic Sensor - Mode 1 Demo
srt04-mode1.ino
Uses JSN-SR04T-V3.0 Ultrasonic Sensor
Displays on Serial Monitor
Mode 1 is set by bridging "M1" pads on board
Also works with A02YYUW Ultrasonic Sensor
DroneBot Workshop 2021
https://dronebotworkshop.com
*/
SerialLogHandler logHandler;
// Include the Software Serial library
// #include <SoftwareSerial.h>
// Define connections to sensor
// int pinRX = 10;
// int pinTX = 11;
// Array to store incoming serial data
unsigned char data_buffer[4] = {0};
char data[256]; // character array to build webhook payload
// Integer to store distance
int distance = 0;
// Variable to hold checksum
unsigned char CS;
// Object to represent software serial port
// SoftwareSerial Serial1(pinRX, pinTX);
void setup() {
// Set up serial monitor
Serial.begin(9600);
// Set up software serial port
Serial1.begin(9600);
}
void loop() {
Log.info("Serial");
// snprintf(data, sizeof(data), "{\"distance\":%d}", distance);
// Log.info(data);
// Run if data available
if (Serial1.available() > 0) {
Log.info("Serial1");
delay(4);
// Check for packet header character 0xff
if (Serial1.read() == 0xff) {
// Insert header into array
data_buffer[0] = 0xff;
// Read remaining 3 characters of data and insert into array
for (int i = 1; i < 4; i++) {
data_buffer[i] = Serial1.read();
}
//Compute checksum
CS = data_buffer[0] + data_buffer[1] + data_buffer[2];
// If checksum is valid compose distance from data
if (data_buffer[3] == CS) {
distance = (data_buffer[1] << 8) + data_buffer[2];
// Print to serial monitor
snprintf(data, sizeof(data), "{\"distance\":%d}", distance);
Log.info(data);
// Serial.print("distance: ");
// Serial.print(distance);
// Serial.println(" mm");
}
}
}
}
NowI am testing this bare bones script and still nothing:
// EXAMPLE USAGE
SerialLogHandler logHandler;
void setup()
{
Serial.begin(9600);
Serial1.begin(9600);
}
void loop()
{
// read from port 0, send to port 1:
if (Serial.available())
{
Log.info("Serial");
int inByte = Serial.read();
Serial1.write(inByte);
}
// read from port 1, send to port 0:
if (Serial1.available())
{
Log.info("Serial1");
int inByte = Serial1.read();
Serial.write(inByte);
}
}
This has brought my project, now at a critical phase, to a standstill. Any help would be appreciated.
philly
Flood Dog Co. www.flooddog.com
Are you sure the RX and TX pins are connected properly? The labeling of these pins is not always consistent, but typically the TX on the breakout is the output from the sensor which goes into the RX pin on the Boron.
When I reverse the TX-RX pins still nothing happens. By nothing I mean it doesn’t seem to get to the line of code that logs “serial1” based on Serial1.available().
This works on an Arduino so I know it’s code that should produce something… ;-0
Is the basic idea of what I’m doing (Serial1.begin(9600)) correct? In. theory. should. this work?
I’m using it in Mode 1 (as per the demo on the Dronebot site). I tested it on an Arduino in mode 1 and then tried to ‘port’ the software over to the Boron replacing mySerial (software serial) with Serial1.
I am using the CLI to monitor the USB serial port on my laptop and I have been trying to send the data to particle in my regular device script but it’s the only variable that is blank.
Just to be sure, the Boron is online and breathing cyan, correct?
Since you’re using the default mode, if it’s blinking dark blue or blinking green it won’t execute the code because you’re not cloud connected. You can allow your code to run always by using
Is there any other suggestions on how to get a serial device two work with a Boron? This is a total brick wall for my project - measuring water levels!
To verify the TX and RX with a meter on DC volts you should see voltage on both the TX and RX pins when connected to the device. If not swap the TX and RX on the sensor and then verify the voltage again. For water level measurements I use the waterproof version of the HC-SR04 with good results.
Sorry I am using JSN-SR04T sensor. I looked at the link and saw it and thought that is the one you are using missing your statement at the beginning of the thread saying you were using the A02YYUW. True serial communications are + - 3 volts minimum. I have seen some at less and the circuit works. With the device disconnected the RX pin on the Particle device I would expect no voltage when serial1 enabled. I have not used the serial settings with a Particle device so am not familiar with the programming side. Reading the sensor info I would connect TX to RX of each device.
Thankfully I have two JSN-SR04T’s for testing so I will go down that road temporarily. My only hesitation stating with this unit is it’s 5v not 3.3v-5v; I do have a boost converter I could use - do you do this?
Thanks again for the feedback
I’m still not sure why Serial doesn’t work in this case - if anyone has more Serial experience than I I would love to hear from. you. I have scoured this Community but found very little directly related.
I've had good results w/ JSN-SR04T when powering from Li-Po voltage (Li+ Pin).
I also seem to remember temporarily using (2) Digital Pins as 3.3V output to power the JSN, but that may have been w/ Mode 3 ?
Looking further on the A02YYUW it doesn’t need any connection from the TX of the Boron to the RX of the sensor. The RX of the sensor is connected to ground or left open to change how often the data is sent. It continually sends data without input from the Boron. I would not connect the TX of the Boron to the sensor, just leave the receive disconnected and it will transmit data every 300ms. Then see if you show receive data on the Boron. The baud rate is correct at 9600 and if I understand the docs on the Boron 8,N,1 is the defaults.
If you have an oscilloscope try connecting it to both devices separately to see if either is transmitting data. This should help you narrow down the issue.
I bought one of the cheaper ultrasonic sensors with a horn from Adafruit. Believe I had a similar issue to yours. The sensor required a baud rate of 9600 so I set Serial1 to that and the USB Serial to 115200 and was able to communicate with both. Hope this helps.
Thanks to both for the last few suggestions. I am now cycling around to this issue and I'll look into it with your comments in mind. I'm still a little baffled but that's the learning part....