In this project we will build a car equipped with an ESP32 card and a hc-06 bluetooth module and controlled by the computer via bluetooth.
The user controls the car using the arrow keys on the keyboard (forward, right, left and back).
car chassis
two wheels
two motors
roulette
9V battery
Connect pin N °5 of the ESP32 board to pin IN1 of the L298N module.
Connect pin N ° 4 of the ESP32 board to pin IN2 of the L298N module.
Connect pin No. 23 of the ESP32 board to pin IN3 of the L298N module.
Connect pin N ° 22 of the ESP32 board to pin IN4 of the L298N module.
Connect the GND pin of the ESP32 board to the GND pin of the L298N module.
Connect the VIN pin of the ESP32 board to the (+) terminal of the power supply module
Connect the GND pin of the ESP32 board to the (-) terminal of the power supply module
Connect the 12V pin of the L298N module to the (+) terminal of the power supply module
Connect the TX pin of the HC-06 module to the RX pin of the ESP32
Connect the RX pin of the HC-06 module to the TX pin of the ESP32
Connect the VCC pin of the HC-06 module to the (+) terminal of the power supply module
Connect the GND pin of the HC-06 module to the (-) terminal of the power supply module
Here is the program which allows to connect the ESP32 card to the HC-06 bluetooth module and to receive a message containing the direction of direction of the car.
Copy the following code to the ESP32 boot.py file.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
from machine import UART from dcmotor import DCMotor from machine import Pin, PWM from time import sleep import time import utime frequency = 15000 pin1 = Pin(5, Pin.OUT) pin2 = Pin(4, Pin.OUT) pin3 = Pin(23, Pin.OUT) pin4 = Pin(22, Pin.OUT) enable = PWM(Pin(18), frequency) enable1 = PWM(Pin(21), frequency) dc_motor = DCMotor(pin1, pin2, enable) dc_motor = DCMotor(pin1, pin2, enable, 350, 1023) dc_motor1 = DCMotor(pin3, pin4, enable1) dc_motor1 = DCMotor(pin3, pin4, enable1, 350, 1023) uart = UART(2, 9600) uart.init(9600, bits=8, parity=None, stop=1) print(uart) while True: if uart.any(): while uart.any(): buf = uart.read() print(str(buf,'utf-8')) if str(buf,'utf-8')=='forward' : dc_motor.forward(100) # car moves forward dc_motor1.forward(100) if str(buf,'utf-8')=='backwards' : dc_motor.backwards(100) # car backs up dc_motor1.backwards(100) if str(buf,'utf-8')=='left' : dc_motor.forward(10) # car turns left dc_motor1.forward(100) if str(buf,'utf-8')=='right' : dc_motor.forward(100) # car turns right dc_motor1.forward(10) if str(buf,'utf-8')=='stop' : dc_motor.stop() # car stops dc_motor1.stop() utime.sleep_ms(10) try: uart.write("OK") print('sent response') except OSError: pass |
Copy the following code into an ESP32 file named dcmotor.py.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
class DCMotor: def __init__(self, pin1, pin2, enable_pin, min_duty=750, max_duty=1023): self.pin1=pin1 self.pin2=pin2 self.enable_pin=enable_pin self.min_duty = min_duty self.max_duty = max_duty def forward(self,speed): self.speed = speed self.enable_pin.duty(self.duty_cycle(self.speed)) self.pin1.value(1) self.pin2.value(0) def backwards(self, speed): self.speed = speed self.enable_pin.duty(self.duty_cycle(self.speed)) self.pin1.value(0) self.pin2.value(1) def stop(self): self.enable_pin.duty(0) self.pin1.value(0) self.pin2.value(0) def duty_cycle(self, speed): if self.speed <= 0 or self.speed > 100: duty_cycle = 0 else: duty_cycle = int(self.min_duty + (self.max_duty - self.min_duty)*((self.speed-1)/(100-1))) return duty_cycle |
This is the program that allows the computer to connect and send data to the HC-06 bluetooth module.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
import serial import time from pynput import keyboard evenement="" print("Start") port="COM20" #This will be different for various devices and on windows it will probably be a COM port. bluetooth=serial.Serial(port, 9600)#Start communications with the bluetooth unit print("Connected") bluetooth.flushInput() #This gives the bluetooth a little kick result = str(0) def on_key_release(key): global result #print('released %s' % key) #result ='released %s' % key+'\n' if result !='stop': #or result == str(2)+'\n' or result == str(3)+'\n' or result == str(4)+'\n': result = 'stop' print(result) result_bytes = result.encode('utf_8') bluetooth.write(result_bytes) def on_key_pressed(key): global result #print('pressed %s' % key) result1 ='%s' % key if result == 'stop': if result1=='Key.up' : result = 'forward' print(result) if result1=='Key.down' : result = 'backwards' print(result) if result1=='Key.left' : result = 'left' print(result) if result1=='Key.right' : result = 'right' print(result) result_bytes = result.encode('utf_8') bluetooth.write(result_bytes) with keyboard.Listener(on_release = on_key_release,on_press=on_key_pressed) as listener: listener.join() |