// This code is based on the example code provided by the manufacturer of the TG633 Ingenious Machines Remote Control Building Kits For Kids.
// Include the necessary libraries for the ESP32 and the motor driver
#include <WiFi.h>
#include <ESP32Servo.h>
// Define the pins for the motor driver
#define ENA 12
#define IN1 13
#define IN2 14
#define IN3 15
#define IN4 16
#define ENB 17
// Define the pins for the remote control receiver
#define CH1 18
#define CH2 19
#define CH3 20
#define CH4 21
// Define the variables for the motor speed and direction
int motorA_speed = 0;
int motorB_speed = 0;
int motorA_direction = 0;
int motorB_direction = 0;
// Define the variables for the remote control channels
int ch1_value = 0;
int ch2_value = 0;
int ch3_value = 0;
int ch4_value = 0;
// Setup function runs once when the board is powered on or reset
void setup() {
// Initialize the serial communication for debugging
Serial.begin(9600);
// Initialize the motor driver pins as outputs
pinMode(ENA, OUTPUT);
pinMode(IN1, OUTPUT);
pinMode(IN2, OUTPUT);
pinMode(IN3, OUTPUT);
pinMode(IN4, OUTPUT);
pinMode(ENB, OUTPUT);
// Initialize the remote control receiver pins as inputs
pinMode(CH1, INPUT);
pinMode(CH2, INPUT);
pinMode(CH3, INPUT);
pinMode(CH4, INPUT);
// Connect to WiFi network
WiFi.begin("Wokwi-GUEST", "" 6); // Replace with your network SSID and password
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.println("Connecting to WiFi...");
}
Serial.println("Connected to WiFi!");
// Start the servo library
Servo.begin();
}
// Loop function runs repeatedly as long as the board has power
void loop() {
// Read the values from the remote control channels
ch1_value = pulseIn(CH1, HIGH);
ch2_value = pulseIn(CH2, HIGH);
ch3_value = pulseIn(CH3, HIGH);
ch4_value = pulseIn(CH4, HIGH);
// Map the values from the remote control channels to the motor speed and direction
motorA_speed = map(ch1_value, 1000, 2000, 0, 255);
motorB_speed = map(ch2_value, 1000, 2000, 0, 255);
motorA_direction = map(ch3_value, 1000, 2000, -1, 1);
motorB_direction = map(ch4_value, 1000, 2000, -1, 1);
// Set the motor speed and direction based on the mapped values
analogWrite(ENA, motorA_speed);
analogWrite(ENB, motorB_speed);
digitalWrite(IN1, motorA_direction);
digitalWrite(IN2, !motorA_direction);
digitalWrite(IN3, motorB_direction);
digitalWrite(IN4, !motorB_direction);
// Print the values for debugging
Serial.print("Motor A Speed: ");
Serial.println(motorA_speed);
Serial.print("Motor B Speed: ");
Serial.println(motorB_speed);
Serial.print("Motor A Direction: ");
Serial.println(motorA_direction);
Serial.print("Motor B Direction: ");
Serial.println(motorB_direction);
// Wait for 100 milliseconds before reading the remote control channels again
delay(100);
}
// Reference: https://www.ingeniousmachines.com/remote-control-building-kits-for-kids/