GenykS
GenykS

Reputation: 1

Connection problem esp32 WROOM with ELRS PWM receiver

I have a problem with writing code for the ESP32 WROOM to process a signal from an ELRS PWM receiver and subsequently control two motors using a driver. The code compiles, but the system monitor shows channel values of 1500 µs and does not respond to the joystick at all.

I tried to write the code using the CRSFFORARDUINO library. Perhaps I need to use a different library, or maybe I chose the wrong baud rate.

#include <CRSFforArduino.hpp>

// Define pins for the MX1508 driver
#define MOTOR1_PWM1 5
#define MOTOR1_PWM2 6
#define MOTOR2_PWM1 9
#define MOTOR2_PWM2 10

// Create an object for working with CRSF
CRSFforArduino crsf;

void setup() {
  // Initialize Serial for data output
  Serial.begin(115200);

  // Initialize CRSF
  crsf.begin(Serial);

  // Configure pins for motor control
  pinMode(MOTOR1_PWM1, OUTPUT);
  pinMode(MOTOR1_PWM2, OUTPUT);
  pinMode(MOTOR2_PWM1, OUTPUT);
  pinMode(MOTOR2_PWM2, OUTPUT);
}

void loop() {
  // Update CRSF data
  crsf.update();

  // Read channel values
  int channel1 = crsf->readRcChannel(2); // Channel for forward/backward control
  int channel2 = crsf->readRcChannel(1); // Channel for left/right control

  // Constrain channel values
  channel1 = constrain(channel1, 1000, 2000);
  channel2 = constrain(channel2, 1000, 2000);

  // Print channel values to the serial monitor
  Serial.print("Channel 1: ");
  Serial.print(channel1);
  Serial.print(" Channel 2: ");
  Serial.println(channel2);

  // Motor control
  if (channel1 > 1550) {
    // Move forward
    int speed = map(channel1, 1550, 2000, 0, 255);
    analogWrite(MOTOR1_PWM1, speed);
    analogWrite(MOTOR1_PWM2, 0);
    analogWrite(MOTOR2_PWM1, speed);
    analogWrite(MOTOR2_PWM2, 0);
  } else if (channel1 < 1450) {
    // Move backward
    int speed = map(channel1, 1000, 1450, 255, 0);
    analogWrite(MOTOR1_PWM1, 0);
    analogWrite(MOTOR1_PWM2, speed);
    analogWrite(MOTOR2_PWM1, 0);
    analogWrite(MOTOR2_PWM2, speed);
  } else {
    // Stop
    analogWrite(MOTOR1_PWM1, 0);
    analogWrite(MOTOR1_PWM2, 0);
    analogWrite(MOTOR2_PWM1, 0);
    analogWrite(MOTOR2_PWM2, 0);
  }

  if (channel2 > 1550) {
    // Turn right
    int speed = map(channel2, 1550, 2000, 0, 255);
    analogWrite(MOTOR1_PWM1, speed);
    analogWrite(MOTOR1_PWM2, 0);
    analogWrite(MOTOR2_PWM1, 0);
    analogWrite(MOTOR2_PWM2, speed);
  } else if (channel2 < 1450) {
    // Turn left
    int speed = map(channel2, 1000, 1450, 255, 0);
    analogWrite(MOTOR1_PWM1, 0);
    analogWrite(MOTOR1_PWM2, speed);
    analogWrite(MOTOR2_PWM1, speed);
    analogWrite(MOTOR2_PWM2, 0);
  }
}

Upvotes: 0

Views: 28

Answers (0)

Related Questions