OK this is a super quick chunk of code that is designed to plug up to a RC speed controller and emulate the signal coming from the receiver.
It’s super handy for those who may be facing issues with their speed controller or the reason I am and that’s currently working on, an RC Diesel engine sound module. Keeping everything on the test bench and no need to take half the kit into the workshop just to prove it works.
The signal is 1500us pulses to 2000us pulses and 1000us pulses to simulate forward and reverse and 1500 is the centre point. This code also implements a web GUI so you will need to input your own SSID and Password and the observe the IP address in Serial Monitor. Once you have gone to the webpage eg: 192.168.1.120 for example you will be presented with a slider to simulate the signal to the speed controller and the attached motor will run accordingly.
Lastly, the code was written for a Wemos D1 Pro Mini but could easily work on a ESp32-Wroom for example. Pin D4 was selected as the Input to the ‘S’ on mose RC controllers. example: – + S where you only need the negative – and S to control the motor from the web GUI.
Happy testing and main me through the contacts page if you need any help but its very basic and most will pick it up in no time.
Code in between asterix’s
***********************************************************************************************************************************************
#include <ESP8266WiFi.h>
#include <ESP8266WebServer.h>
const char* ssid = “YOUR_WIFI_SSID”;
const char* password = “YOUR_WIFI_PASSWORD”;
ESP8266WebServer server(80);
const int servoPin = D4; // You can use any PWM-capable pin (D1–D8 except D0 (or D zero))
int pulseWidth = 1500; // Default pulse width in microseconds
void setup() {
Serial.begin(115200);
pinMode(servoPin, OUTPUT);
// Connect to Wi-Fi
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.println(“Connecting to WiFi…”);
}
Serial.println(“Connected to Wi-Fi”);
Serial.print(“IP Address: “);
Serial.println(WiFi.localIP());
// Start web server
server.on(“/”, handleRoot);
server.on(“/update”, handleUpdate);
server.begin();
}
void loop() {
server.handleClient();
sendServoPulse(pulseWidth); // Continuously send pulse
delay(20); // 50 Hz refresh rate
}
void sendServoPulse(int width) {
digitalWrite(servoPin, HIGH);
delayMicroseconds(width);
digitalWrite(servoPin, LOW);
}
void handleRoot() {
String html = R”rawliteral(
<!DOCTYPE html>
<html>
<head>
<title>RC Signal Control</title>
<meta name=”viewport” content=”width=device-width, initial-scale=1″>
<style>
body { text-align: center; font-family: ‘Arial’; }
.slider { width: 80%; }
</style>
</head>
<body>
<h1>G5STO’s – RC Signal Control</h1>
<p>Adjust the slider to change the signal sent to the sound module.</p>
<p>Pulse Width: <span id=”pulseValue”>1500</span> μs</p>
<input type=”range” min=”1000″ max=”2000″ value=”1500″ class=”slider” id=”myRange” oninput=”updateValue(this.value)”>
<script>
function updateValue(value) {
document.getElementById(‘pulseValue’).innerHTML = value;
var xhttp = new XMLHttpRequest();
xhttp.open(“GET”, “/update?value=” + value, true);
xhttp.send();
}
</script>
</body>
</html>
)rawliteral”;
server.send(200, “text/html”, html);
}
void handleUpdate() {
int value = server.arg(“value”).toInt();
if (value >= 1000 && value <= 2000) {
pulseWidth = value;
server.send(200, “text/plain”, String(pulseWidth));
} else {
server.send(400, “text/plain”, “Invalid pulse width”);
}
}
*************************************************************************************************************************************************