cancel
Showing results for 
Search instead for 
Did you mean: 

No Data Received from RS485 Sensor via UART on STM32L0 Using serial.available

shibani
Associate II

I’m interfacing a JXBS-3001-RS Soil Sensor with an STM32L0 board using an RS485 transceiver with automatic direction control. While I can successfully send the request command to the sensor, I’m facing an issue receiving the sensor’s data.

Setup:

  • Microcontroller: STM32L0 series
  • Sensor: JXBS-3001-RS Soil Sensor
  • Communication: RS485 via UART
  • RS485 Transceiver: Auto-direction control IC (MAX13487EESA T)

Issue:

I’m using Serial.Available() to determine when data is available from the sensor, but the condition for data availability is never true, so I cannot read any data. Code block is shown below:

startTime = milis();
while (milis() - startTime <= 500) {
if (Serial.Available() > 0) {
if (byteCount < sizeof(values_temp)) {
values_temp[byteCount++] = Serial.Read();
}
}
}

Additionally, I’ve verified the sensor’s output using a TTL-to-USB adapter and a logic analyzer, and I can see that the sensor is sending the correct data. However, the STM32L0 is still not detecting this incoming data.

To troubleshoot, I tested the same approach on a Raspberry Pi Pico, using the same RS485 transceiver and sensor. With the Pico, Serial.Available() behaves as expected, and it detects incoming data from the sensor correctly.

What I've Tried:

  1. Ensured the UART configuration (baud rate, stop bits, and parity) matches the sensor’s communication requirements.
  2. Tested both manual and automatic RS485 direction control.
  3. Verified that data is being sent by the sensor using a logic analyzer and a TTL adapter.
  4. Confirmed the same setup works on a Raspberry Pi Pico, where Serial.Available() successfully detects the data.

Request:

Given that the same setup works on a Raspberry Pi Pico but not on the STM32L0, what could be causing the STM32 UART to miss the incoming data? I’d appreciate any suggestions for correctly configuring UART to receive RS485 data on the STM32L0.

7 REPLIES 7
Pavel A.
Evangelist III

What is this Serial.Available?  Some Arduino-like C++ library for STM32? STM32duino?

> Verified that data is being sent by the sensor using a logic analyzer and a TTL adapter.

On STM32L0?

 

>>.. what could be causing the STM32 UART to miss the incoming data?

Signs would point to your board level driver implementation in Arduino, likely not being aware of the half-duplex operation, or the TX reflecting back into the RX, or how to manage the RX vs TX cases.

Review the board's variant code.. debug that.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..

Actually, I’ve implemented my own SerialAvailable() function for handling UART reception with a circular buffer. Here’s the function I’m using:

int SerialAvailable() {
return (RX_BUFFER_SIZE_1 + rx_buffer_head1 - rx_buffer_tail1) % RX_BUFFER_SIZE_1;
}
This function calculates the number of bytes available in the receive buffer by comparing the head and tail indices. I’m using a circular buffer for UART reception, and this function helps me determine how much data is currently available to read.

Even with this implementation, I’m facing the issue where no data seems to be received by the STM32L0. However, as I mentioned earlier, the sensor data is verified via a TTL-to-USB adapter and logic analyzer.

Pavel A.
Evangelist III

If receive ceases for no clear reason - check for TX overrun condition & reset it.

 

Thank you for your suggestion regarding the TX overrun condition. I would like to clarify that when I encounter a TX overrun error, how can I reset it?

Additionally, I noticed that when I transmit the command to the RS485 sensor, instead of receiving the expected sensor data, I receive the command back. What could be causing this issue, and how can I ensure the sensor responds correctly?

I want to clarify that I am not using an Arduino-level driver. Instead, I have implemented a custom driver for the STM32 with a circular buffer for serial communication.

I am currently using auto direction control to manage the transmit and receive modes for my RS485 communication. Given the issues I'm experiencing with receiving sensor data, do I need to implement manual direction control instead? Would manually setting the direction enhance the reliability of communication in my setup?

Andrew Neil
Evangelist III

@shibani wrote:

I’ve verified the sensor’s output using a TTL-to-USB adapter and a logic analyzer, and I can see that the sensor is sending the correct data. However, the STM32L0 is still not detecting this incoming data.


So have you confirmed that the data is actually reaching the STM32L0's UART receive pin?