I am trying to read data from Serial2 on Serial and I'm only getting nonsense. I am reading an SHT-30 Humidy sensor from an Adafruit Feather nRF52840 Sense with an Adafruit Feather M0 RFM96 LoRa Radio - 433MHz. I am trying to write it to Serial2 and then read it from Serial to learn how to use multiple Serial ports on one MCU.
#include <Arduino.h> // required before wiring_private.h
#include "wiring_private.h" // pinPeripheral() function
#include <Wire.h>
#include <Adafruit_SHT31.h>
#include "avr/dtostrf.h"
Adafruit_SHT31 sht30; // humidity
Uart Serial2 (&sercom2, 3, 4, SERCOM_RX_PAD_1, UART_TX_PAD_0);
void SERCOM2_Handler()
{
Serial2.IrqHandler();
}
void setup() {
sht30.begin();
Serial.begin(115200);
Serial2.begin(115200);
// Assign pins 3 & 4 SERCOM functionality
pinPeripheral(3, PIO_SERCOM_ALT);
pinPeripheral(4, PIO_SERCOM_ALT);
}
float incomingByte = 0;
void loop() {
char c[1] = {};
dtostrf(sht30.readHumidity(), 5, 2, c);
Serial2.write(c);
incomingByte = Serial2.read();
Serial.println(incomingByte);
Serial.println("Regular Serial Print");
Serial.println(sht30.readHumidity());
delay(500);
}
Serial Monitor Output
12:15:18.457 -> -1.00
12:15:18.457 -> Regular Serial Print
12:15:18.504 -> 46.80
12:15:19.018 -> -1.00
12:15:19.018 -> Regular Serial Print
12:15:19.018 -> 46.77
It's only reading "-1.00" from the Serial2 but reading accurately on Serial. I followed the tutorial for making a second Serial port for my MCU but I still can't get reads from Serial2. I'm assuming there is some formatting error but I haven't found it yet. I want it to read the same on Serial and Serial2. My end goal is to have a GPS module on Serial2 but I am starting with the humidity sensor since its easier to work with and gives me data faster.