math1518
Hello everyone, I'm using a Raspberry Pi 5 for a school project to build a robot. One part of the project involves making the robot spin in place for a few seconds randomly. For this, I have 2 small 6V motors, a 6V battery, an L298n H-bridge, and a Raspberry Pi 5. The 2 motors are connected to the H-bridge at OUTPUT 1 and 2, and OUTPUT 3 and 4, respectively. The battery is connected to 12V for the positive and GND for the negative. For the other pins, EVA and EVB are connected to PIN 2 and 4 of the board (5V no PWM), IN1 with GPIO26, IN2 with GPIO27, IN3 with GPIO6, and IN4 with GPIO7.
Here is the code used to test the motors:
'from gpiozero import Motor
from time import sleep
import random
Motor configuration
motor_left = Motor(forward=26, backward=27)
motor_right = Motor(forward=5, backward=7)
try:
while True:
# Random duration between 5 and 15 seconds
spin_time = random.uniform(5, 15)
print(f"The robot spins for {spin_time:.2f} seconds")
# Spin the robot in place
motor_left.forward()
motor_right.backward()
Wait for the random duration
sleep(spin_time)
Stop the robot
motor_left.stop()
motor_right.stop()
Optional: Wait for a random duration between spins
rest_time = random.uniform(1, 5)
print(f"Resting for {rest_time:.2f} seconds")
sleep(rest_time)
except KeyboardInterrupt:
print("User interrupted")'
When this code is executed, it displays the prints correctly but the motors do not spin, and I can't figure out where the problem is coming from. If you have any ideas to fix the issue, I'd appreciate it! Thanks in advance!