Raspberry Pi Control Servo Motor via Bluetooth

This tutorial instructs you how to program a Raspberry Pi to manage a Servo Motor by utilizing either Bluetooth (HC-05 module) or BLE (HM-10 module). Step-by-step instructions for both modules are given.

We will use the Bluetooth Serial Monitor App on a smartphone to transmit the angle value to Raspberry Pi. Raspberry Pi will adjust the servo motor based on the received value.

Raspberry Pi Servo Motor Bluetooth

Hardware Preparation

1×Raspberry Pi 4 Model B
1×HC-05 Bluetooth Module
1×(Alternative) HM-10 BLE Module
1×Servo Motor
1×Jumper Wires
1×(Optional) Screw Terminal Adapter for Raspberry Pi
1×(Optional) Power Adapter for Raspberry Pi 4B
1×(Optional) Plastic Case for Raspberry Pi 4B

Or you can buy the following sensor kits:

1×DIYables Sensor Kit (30 sensors/displays)
1×DIYables Sensor Kit (18 sensors/displays)
Disclosure: Some of the links provided in this section are Amazon affiliate links. We may receive a commission for any purchases made through these links at no additional cost to you.
Additionally, some of these links are for products from our own brand, DIYables.

Overview of Servo Motor and Bluetooth Module

If you are not familiar with Servo Motors, Bluetooth Modules, their pinouts, capabilities, and programming, please refer to the following tutorials for more information:

Wiring Diagram

  • To manage a Servo Motor with Classic Bluetooth, the HC-05 Bluetooth module should be used and the wiring diagram provided should be consulted.
The wiring diagram between Raspberry Pi and Servo Motor Bluetooth

This image is created using Fritzing. Click to enlarge image

  • To manage a Servo Motor with BLE, the HM-10 BLE module should be used. The wiring diagram is given below for reference.
The wiring diagram between Raspberry Pi and Servo Motor BLE

This image is created using Fritzing. Click to enlarge image

Raspberry Pi Code - controls Servo Motor via Bluetooth/BLE

The code given here is able to be used with both the HC-10 Bluetooth module and the HM-10 BLE module.

Detailed Instructions

  • Make sure you have Raspbian or any other Raspberry Pi compatible operating system installed on your Pi.
  • Make sure your Raspberry Pi is connected to the same local network as your PC.
  • Make sure your Raspberry Pi is connected to the internet if you need to install some libraries.
  • If this is the first time you use Raspberry Pi, See how to set up the Raspberry Pi
  • Connect your PC to the Raspberry Pi via SSH using the built-in SSH client on Linux and macOS or PuTTY on Windows. See to how connect your PC to Raspberry Pi via SSH.
  • Make sure you have the RPi.GPIO library installed. If not, install it using the following command:
sudo apt-get update sudo apt-get install python3-rpi.gpio
pip install pyserial
  • Create a Python script file bluetooth_servo.py and add the following code:
""" This Raspberry Pi code was developed by newbiely.com This Raspberry Pi code is made available for public use without any restriction For comprehensive instructions and wiring diagrams, please visit: https://newbiely.com/tutorials/raspberry-pi/raspberry-pi-control-servo-motor-via-bluetooth """ import serial import RPi.GPIO as GPIO from time import sleep # Define pins SERVO_PIN = 16 # GPIO 16 (Servo pin) # Set up GPIO GPIO.setmode(GPIO.BCM) GPIO.setup(SERVO_PIN, GPIO.OUT) # Create serial port for Bluetooth communication bluetooth = serial.Serial('/dev/ttyS0', baudrate=9600, timeout=1) # Create servo object servo = GPIO.PWM(SERVO_PIN, 50) # 50 Hz frequency for standard servo # Start PWM with 0% duty cycle (servo at 0 degrees) servo.start(0) def rotate_servo(angle): duty_cycle = 2 + (angle / 18) # Map angle to duty cycle (for a standard servo) servo.ChangeDutyCycle(duty_cycle) sleep(1) # Give servo time to move # Main program try: bluetooth.flushInput() # Clear any existing data in the input buffer while True: bluetooth.write(b"Raspberry Pi here, command me!\n") if bluetooth.inWaiting() > 0: angle = bluetooth.parseInt() if 0 <= angle <= 180: rotate_servo(angle) bluetooth.write(b"Rotated servo to angle: ") bluetooth.write(str(angle).encode('utf-8')) bluetooth.write(b"\n") else: bluetooth.write(b"Invalid angle: ") bluetooth.write(str(angle).encode('utf-8')) bluetooth.write(b"\n") # Add a delay to avoid excessive looping sleep(0.1) except KeyboardInterrupt: pass finally: servo.stop() GPIO.cleanup() bluetooth.close()
  • Save the file and run the Python script by executing the following command in the terminal:
python3 bluetooth_servo.py

The script runs in an infinite loop continuously until you press Ctrl + C in the terminal.

  • Install the Bluetooth Serial Monitor App on your smartphone.
  • Open the Bluetooth Serial Monitor App on your smartphone and choose either the Classic Bluetooth or BLE option, depending on the module you are using.
Bluetooth Serial Monitor App
  • Connect the app to the HC-05 Bluetooth module or HM-10 BLE module.
Bluetooth Serial Monitor pairing
  • Enter an angle, such as 45 or 90, and then press the Send button.
Bluetooth Serial Monitor App
  • Witness the Servo Motor's angle alteration.
  • Examine the outcomes on the Android App.
Bluetooth Serial Monitor App

If you find the Bluetooth Serial Monitor app helpful, please rate it 5 stars on Play Store. Thank you for your support!

Video Tutorial

※ OUR MESSAGES

  • As freelancers, We are AVAILABLE for HIRE. See how to outsource your project to us
  • Please feel free to share the link of this tutorial. However, Please do not use our content on any other websites. We invested a lot of effort and time to create the content, please respect our work!