Hello everyone, I am encountering an issue using an HC12 transmitter receiver, a Servo and an ESC.
I use them to communicate with my RC car, remotely.
Issue: On the receiving end, my servomotor jerks and strange characters appear on my serial monitor.
Here is the wiring diagram :
here's a photo of the assembly:
Here is the code I use for reception:
#include <Servo.h>
#include <SoftwareSerial.h>
// Pins for HC-12
const int HC12_TX_PIN = 3; // HC-12 TX to Arduino D3
const int HC12_RX_PIN = 2; // HC-12 RX to Arduino D2
// Pin for ESC and Servo
const int SERVO_PIN = 5;
SoftwareSerial HC12(HC12_TX_PIN, HC12_RX_PIN);
Servo esc; // Create a Servo object for the ESC
void setup() {
// Start the serial communication with HC-12
HC12.begin(9600);
// Start the serial communication with the computer (for debugging)
Serial.begin(9600);
// Attach the servo to the pin
myServo.attach(SERVO_PIN);
// Attach the ESC to the pin
}
void loop() {
if (HC12.available()) {
// Read the incoming string
String dataReceived = HC12.readStringUntil('\n');
Serial.println(dataReceived);
// For debugging
// Parse the string to get the motor power and servo position
int commaIndex = dataReceived.indexOf(',');
if (commaIndex > 0) {
int motorPower = dataReceived.substring(0, commaIndex).toInt();
int posServo = dataReceived.substring(commaIndex + 1).toInt();
// Set the servo position
myServo.write(posServo);
delay(100);
}
}
}
I receive a string whith this format : "x, y" with x the motor power (we don't care) and y the servo position
When no servo is connected, I receive well the data like this (serial monitor):
91,22
91,22
91,22
91,22
But when the servo is connected, the data start to jerk like this (serial monitor):
����91,22
��������������������91,22
91,22
91,22
������91,22
91,22
91,22
91,22
�������91,22
91,22
91,22
91,22
��������
whenever I receive these weird caracters, the servo starts to jerk and i want to avoid that. I checked the voltage of the servo and it oscillates between4.2 and 4.8 V
Have you ever encountered such an issue ? Feel free to ask me further information.
Thank you in advance !