✦ VidyaVerseOpen interactive version

← Python Lab Practical Record

EXPERIMENT No. 8

Python Lab Practical Record · Python programming laboratory

Read with diagrams, search and self-test

8A. Controlling a DC motor using the L298N motor driver with Raspberry Pi

Aim

To control a DC motor using the L298N motor driver with a Raspberry Pi.

Connections

L298N pinConnected to
IN1Raspberry Pi GPIO 17 (forward)
IN2Raspberry Pi GPIO 18 (backward)
ENAJumper fitted (full speed)
OUT1, OUT2DC motor terminals
+12 V, GNDExternal motor supply (+), (−)
GNDRaspberry Pi GND (common ground)

Program (dc_motor_l298n.py)

# Exp 8A: Controlling a DC motor using L298N motor driver with Raspberry Pi
from gpiozero import Motor
from time import sleep
motor = Motor(forward=17, backward=18)
motor.forward()
print("Motor rotating forward")
sleep(5)
motor.stop()
print("Motor stopped")

Output

Motor rotating forward
Motor stopped

Result

The DC motor was rotated forward for 5 seconds and then stopped using the L298N driver and Raspberry Pi.

8B. Simple automated systems – fan and water pump controllers

Aim

To build simple automated systems like a fan and a water pump controller.

Formula / Note

Program 1 switches the fan ON when the temperature exceeds 30 °C. Program 2 switches the water pump ON when the water level is below 30 %.

Program 1 (fan_controller.py)

# Exp 8B (Program 1): Automatic fan controller
from gpiozero import Motor
temperature = 35
fan = Motor(forward=17, backward=18)
if temperature > 30:
    fan.forward()
    print("Fan ON")
else:
    fan.stop()
    print("Fan OFF")

Output 1

Fan ON

Program 2 (water_pump_controller.py)

# Exp 8B (Program 2): Automatic water pump controller
water_level = 20
if water_level < 30:
    print("Water pump ON")
else:
    print("Water pump OFF")

Output 2

Water pump ON

Result

A temperature-based fan controller and a level-based water pump controller were built.