Hi everyone I am trying to encode a sbus signal where the channel values are taken from few joysticks and switches states. The problem I facing now i have written the code and to verify if its a sbus signal I am interfacing this output TX pin(pin 1) of uno to my flight controller which is used in drones.
I am not able to see any movements in channel value. even when I set the frame to send 1300 pwm in all channels there is no data in mission planner.
// Pin Definitions
const int sbusPin = 1; // TX Pin for SBUS output (Use TX Pin 1)
// SBUS min and max values (for mapping to 11-bit SBUS values)
const int sbusMin = 172; // Corresponds to ~1000 PWM (SBUS units)
const int sbusMax = 1815; // Corresponds to ~2000 PWM (SBUS units)
// Function to send SBUS frame
void sendSBUSFrame(uint16_t channels[16]) {
uint8_t sbusFrame[25] = {0};
// Start byte
sbusFrame[0] = 0x0F;
// 11-bit encoding for each channel
sbusFrame[1] = (channels[0] & 0x07FF); // First 8 bits of Channel 1
sbusFrame[2] = ((channels[0] >> 8) | (channels[1] << 3)) & 0xFF; // Remaining bits of Channel 1 and first bits of Channel 2
sbusFrame[3] = ((channels[1] >> 5) | (channels[2] << 6)) & 0xFF; // Remaining bits of Channel 2 and first bits of Channel 3
sbusFrame[4] = (channels[2] >> 2) & 0xFF; // Remaining bits of Channel 3
sbusFrame[5] = ((channels[2] >> 10) | (channels[3] << 1)) & 0xFF;
sbusFrame[6] = ((channels[3] >> 7) | (channels[4] << 4)) & 0xFF;
sbusFrame[7] = ((channels[4] >> 4) | (channels[5] << 7)) & 0xFF;
sbusFrame[8] = (channels[5] >> 1) & 0xFF;
sbusFrame[9] = ((channels[5] >> 9) | (channels[6] << 2)) & 0xFF;
sbusFrame[10] = ((channels[6] >> 6) | (channels[7] << 5)) & 0xFF;
sbusFrame[11] = (channels[7] >> 3) & 0xFF;
sbusFrame[12] = (channels[8] & 0xFF);
sbusFrame[13] = ((channels[8] >> 8) | (channels[9] << 3)) & 0xFF;
sbusFrame[14] = ((channels[9] >> 5) | (channels[10] << 6)) & 0xFF;
sbusFrame[15] = (channels[10] >> 2) & 0xFF;
sbusFrame[16] = ((channels[10] >> 10) | (channels[11] << 1)) & 0xFF;
sbusFrame[17] = ((channels[11] >> 7) | (channels[12] << 4)) & 0xFF;
sbusFrame[18] = ((channels[12] >> 4) | (channels[13] << 7)) & 0xFF;
sbusFrame[19] = (channels[13] >> 1) & 0xFF;
sbusFrame[20] = ((channels[13] >> 9) | (channels[14] << 2)) & 0xFF;
sbusFrame[21] = ((channels[14] >> 6) | (channels[15] << 5)) & 0xFF;
sbusFrame[22] = (channels[15] >> 3) & 0xFF;
// Flags byte (no failsafe or frame lost)
sbusFrame[23] = 0x00;
// End byte
sbusFrame[24] = 0x00;
// Send the SBUS frame
Serial.write(sbusFrame, 25);
}
void setup() {
// Initialize the serial port for SBUS output
Serial.begin(100000, SERIAL_8E2); // SBUS requires 100k baud, 8 data bits, even parity, 2 stop bits
}
void loop() {
uint16_t channels[16];
for (int i = 0; i < 16; i++) {
channels[i] = 992; // Corresponds to 1300 PWM (in SBUS units)
}
// Send the SBUS frame
sendSBUSFrame(channels);
// Print debug information
Serial.print("1: ");
Serial.print(channels[0]);
Serial.print(" 2: ");
Serial.println(channels[1]);
delay(14); // Send SBUS frames every ~14ms (~72 Hz)
}