I am attempting to establish a Uart connection between an ESP32C3 XIAO seeeduino and a WT901B Wit-Motion accel, gyro, etc, sensor. I ended up getting a handshake with the two being wired but when I tried implementing them with a custom PCB it doesn't work. I found the issue with the PCB, the RX and TX were not connected properly as the photo I had used for reference was mirrored and I hadn't noticed, anyways I removed the tracers and just wired them but it still isn't working. I tried different serial ports on the ESP but it just wont connect. I really don't know what the issue could be I have tried a lot and I'm not sure what I'm missing. I think it's possible its my code but I didn't change it from what it was in working condition. My code consists of parts of the Wit-Motion supplied code and a timer among a few other things. I was also running into issues with the serial monitor, after about 5-10 seconds it stops displaying anything. I ended up using puTTy for reading it but I don't know why Serial monitor isn't working.
-Upload Settings-
USB CDC on boot -enabled
CPU frequency -160MHz
Core Debug -none
Erase All Flash... -enabled
Flash Frequency -80MHz
Flash Mode -QIO (I tried DIO too but didn't make a difference)
Flash Size -4MB
Upload Speed -512000 (I tried lowering but it just changes it back)
Programmer -Epstool
Code:
#include <REG.h>
#include <wit_c_sdk.h>
#include <HardwareSerial.h>
#include <chrono>
#include <Arduino.h>
bool UP = false;
bool DOWN = false;
bool NEUTRAL = false;
bool neutralOccurred = false;
#define ACC_UPDATE 0x01
#define GYRO_UPDATE 0x02
#define ANGLE_UPDATE 0x04
#define MAG_UPDATE 0x08
#define READ_UPDATE 0x80
static volatile char s_cDataUpdate = 0, s_cCmd = 0xff;
HardwareSerial MySerial0(0);
static void CmdProcess(void);
static void AutoScanSensor(void);
static void SensorUartSend(uint8_t *p_data, uint32_t uiSize);
static void SensorDataUpdata(uint32_t uiReg, uint32_t uiRegNum);
static void Delayms(uint16_t ucMs);
const uint32_t c_uiBaud[8] = {115200};
class Timer {
public:
Timer() : m_running(false), m_elapsedTime(0) {}
void start() {
if (!m_running) {
m_startTime = millis();
m_running = true;
}
}
void stop() {
if (m_running) {
m_elapsedTime += millis() - m_startTime;
m_running = false;
}
}
void reset() {
m_elapsedTime = 0;
m_running = false;
}
unsigned long getElapsedTime() const {
if (m_running) {
return m_elapsedTime + (millis() - m_startTime);
} else {
return m_elapsedTime;
}
}
private:
bool m_running;
unsigned long m_startTime;
unsigned long m_elapsedTime; // in milliseconds
};
Timer timer;
//********************
void setup() {
Serial.begin(9600);
MySerial0.begin(115200, 20, 21);
WitInit(WIT_PROTOCOL_NORMAL, 0x50);
WitSerialWriteRegister(SensorUartSend);
WitRegisterCallBack(SensorDataUpdata);
WitDelayMsRegister(Delayms);
Serial.print("\r\n********************** wit-motion normal example ************************\r\n");
AutoScanSensor();
}
int i;
float fAcc[3], fGyro[3], fAngle[3];
void loop() {
while (MySerial0.available())
{
WitSerialDataIn(MySerial0.read());
while (Serial.available())
{
CopeCmdData(Serial.read());
}
CmdProcess();
if(s_cDataUpdate)
{
for(i = 0; i < 3; i++)
{
fAcc[i] = sReg[AX+i] / 32768.0f * 16.0f;
fGyro[i] = sReg[GX+i] / 32768.0f * 2000.0f;
fAngle[i] = sReg[Roll+i] / 32768.0f * 180.0f;
}
if(s_cDataUpdate & ACC_UPDATE)
{
Serial.print("acc:");
Serial.print(fAcc[0], 3);
Serial.print(" ");
Serial.print(fAcc[1], 3);
Serial.print(" ");
Serial.print(fAcc[2], 3);
Serial.print("\r\n");
s_cDataUpdate &= ~ACC_UPDATE;
}
s_cDataUpdate = 0;
}
if (sqrt((fAcc[0]*fAcc[0])+(fAcc[1]*fAcc[1])+(fAcc[2]*fAcc[2])) > 1.05)
{
DOWN = true;
UP = false;
NEUTRAL = false;
Serial.print("Down-");
}
if (sqrt((fAcc[0]*fAcc[0])+(fAcc[1]*fAcc[1])+(fAcc[2]*fAcc[2])) < .95)
{
UP = true;
DOWN = false;
NEUTRAL = false;
Serial.print("Up+");
}
if ( (sqrt((fAcc[0]*fAcc[0])+(fAcc[1]*fAcc[1])+(fAcc[2]*fAcc[2])) < 1.05) && (sqrt((fAcc[0]*fAcc[0])+(fAcc[1]*fAcc[1])+(fAcc[2]*fAcc[2])) > .95) )
{
NEUTRAL = true;
UP = false;
DOWN = false;
Serial.print("Neutral=");
}
handleEvents(UP, NEUTRAL);
handleEvents2(DOWN, NEUTRAL);
Show();
}
}
//*******************************
void handleEvents(bool UP, bool NEUTRAL) {
static bool upDetected = false; // Flag to track if UP has been detected
if (UP && !upDetected) {
upDetected = true; // Set UP detected
} else if (upDetected && NEUTRAL) {
// If UP has been detected and NEUTRAL occurs, perform the action
timer.stop();
upDetected = false; // Reset UP detection
} else if (!UP && NEUTRAL) {
// If NEUTRAL occurs without UP, reset UP detection
upDetected = false;
}
}
//*******************************
void handleEvents2(bool DOWN, bool NEUTRAL) {
static bool downDetected = false; // Flag to track if DOWN has been detected
if (DOWN && !downDetected) {
if (neutralOccurred) {
// If DOWN event occurs after NEUTRAL, start the timer
timer.reset();
}
downDetected = true; // Set DOWN detected
} else if (downDetected && NEUTRAL) {
// If DOWN has been detected and NEUTRAL occurs, perform the action
timer.start();
downDetected = false; // Reset DOWN detection
}
// Update the state of NEUTRAL event
neutralOccurred = NEUTRAL;
}
void Show()
{
Serial.print(timer.getElapsedTime());
Serial.print(" , milliseconds : ");
delay(50);
}
//******************************
void CopeCmdData(unsigned char ucData)
{
static unsigned char s_ucData[50], s_ucRxCnt = 0;
s_ucData[s_ucRxCnt++] = ucData;
if(s_ucRxCnt<3)return; //Less than three data returned
if(s_ucRxCnt >= 50) s_ucRxCnt = 0;
if(s_ucRxCnt >= 3)
{
if((s_ucData[1] == '\r') && (s_ucData[2] == '\n'))
{
s_cCmd = s_ucData[0];
memset(s_ucData,0,50);
s_ucRxCnt = 0;
}
else
{
s_ucData[0] = s_ucData[1];
s_ucData[1] = s_ucData[2];
s_ucRxCnt = 2;
}
}
}
static void CmdProcess(void)
{
switch(s_cCmd)
{
case 'a': if(WitStartAccCali() != WIT_HAL_OK) Serial.print("\r\nSet AccCali Error\r\n");
break;
case 'm': if(WitStartMagCali() != WIT_HAL_OK) Serial.print("\r\nSet MagCali Error\r\n");
break;
case 'e': if(WitStopMagCali() != WIT_HAL_OK) Serial.print("\r\nSet MagCali Error\r\n");
break;
case 'u': if(WitSetBandwidth(BANDWIDTH_5HZ) != WIT_HAL_OK) Serial.print("\r\nSet Bandwidth Error\r\n");
break;
case 'U': if(WitSetBandwidth(BANDWIDTH_256HZ) != WIT_HAL_OK) Serial.print("\r\nSet Bandwidth Error\r\n");
break;
case 'B': if(WitSetUartBaud(WIT_BAUD_115200) != WIT_HAL_OK) Serial.print("\r\nSet Baud Error\r\n");
else
{
MySerial0.begin(c_uiBaud[WIT_BAUD_115200]);
Serial.print(" 115200 Baud rate modified successfully\r\n");
}
break;
case 'b': if(WitSetUartBaud(WIT_BAUD_9600) != WIT_HAL_OK) Serial.print("\r\nSet Baud Error\r\n");
else
{
MySerial0.begin(c_uiBaud[WIT_BAUD_9600]);
Serial.print(" 9600 Baud rate modified successfully\r\n");
}
break;
case 'r': if(WitSetOutputRate(RRATE_1HZ) != WIT_HAL_OK) Serial.print("\r\nSet Baud Error\r\n");
else Serial.print("\r\nSet Baud Success\r\n");
break;
case 'R': if(WitSetOutputRate(RRATE_10HZ) != WIT_HAL_OK) Serial.print("\r\nSet Baud Error\r\n");
else Serial.print("\r\nSet Baud Success\r\n");
break;
case 'C': if(WitSetContent(RSW_ACC|RSW_GYRO|RSW_ANGLE|RSW_MAG) != WIT_HAL_OK) Serial.print("\r\nSet RSW Error\r\n");
break;
case 'c': if(WitSetContent(RSW_ACC) != WIT_HAL_OK) Serial.print("\r\nSet RSW Error\r\n");
break;
default :break;
}
s_cCmd = 0xff;
}
static void SensorUartSend(uint8_t *p_data, uint32_t uiSize)
{
MySerial0.write(p_data, uiSize);
MySerial0.flush();
}
static void Delayms(uint16_t ucMs)
{
delay(ucMs);
}
static void SensorDataUpdata(uint32_t uiReg, uint32_t uiRegNum)
{
int i;
for(i = 0; i < uiRegNum; i++)
{
switch(uiReg)
{
case AZ:
s_cDataUpdate |= ACC_UPDATE;
break;
case GZ:
s_cDataUpdate |= GYRO_UPDATE;
break;
case HZ:
s_cDataUpdate |= MAG_UPDATE;
break;
case Yaw:
s_cDataUpdate |= ANGLE_UPDATE;
break;
default:
s_cDataUpdate |= READ_UPDATE;
break;
}
uiReg++;
}
}
static void AutoScanSensor(void)
{
int i, iRetry;
for(i = 0; i < sizeof(c_uiBaud)/sizeof(c_uiBaud[0]); i++)
{
MySerial0.begin(c_uiBaud[i]);
MySerial0.flush();
iRetry = 2;
s_cDataUpdate = 0;
do
{
WitReadReg(AX, 3);
delay(200);
while (MySerial0.available())
{
WitSerialDataIn(MySerial0.read());
}
if(s_cDataUpdate != 0)
{
Serial.print(c_uiBaud[i]);
Serial.print(" baud find sensor\r\n\r\n");
return ;
}
iRetry--;
}while(iRetry);
}
Serial.print("can not find sensor\r\n");
Serial.print("please check your connection\r\n");
}
This is the PCB (the WT901B is getting power):