Friday, September 16, 2022

Simple Long Range Radio from Raspberry Pi to raspberry Pi Pico using HC-12 433MHz

I have a project in progress that is the third generation of my Hen House Door project. I want to be able to open and close the door from my home automation controller (Raspberry Pi 2 running NodeRED). Unfortunately, the hen house is at the end of the garden and out of WiFi range. Anyway the door opener is low-power and solar powered, and WiFi uses too much current.

I've used NRF24 and CC1101 modules before and frankly there are a lot of wires to connect for SPI and generally a mess of libraries to try and get working. Wouldn't it be lovely (I mused) if there was a long range wireless module that talked UART serial and hid all the communication layer stuff. 

With a bit of googling, I was delighted to find the HC-12 modules that do exactly that. They take AT commands to configure them for power mode etc. But aside from that they work just like you had a cable between your two devices and talked serial along it.

Eventually, my Raspberry Pi 2 will talk to the hen house controller that is based on an ATTiny1616 at 1MHz and using a minimal amount of electricity except when driving the door motor. But as a first test, I thought it would be good to establish a link between a Pico and a regular Raspberry Pi (in this case, my Pi 400).

The Pico

Here's the Pico end.

The connections are:

  • GND on the Pico to GND on the HC-12
  • 3V on the Pico to 3V on the HC-12
  • Tx on the Pico to Rx on the HC-12
  • Rx on the Pico to Tx on the HC-12
To test it out, I used the program below - making sure its copied onto the Pico itself (use Save a Copy in Thonny) so I can take it mobile with a battery pack.

Here's the code for ease of copying:

from machine import UART
import time

uart = UART(0, 9600, timeout=400)

count = 0

while True:
    count += 1

The Raspberry Pi

Here's an HC-12 attached to my Pi400 by way of a GPIO adapter and some jumper wires.

The connections are:

  • GND on the Pi to GND on the HC-12
  • 3V on the Pi to 3V on the HC-12
  • Tx on the Pi (14) to Rx on the HC-12
  • Rx on the Pi ( 15) to Tx on the HC-12
The receiving code is just:

import serial

ser = serial.Serial('/dev/serial0', baudrate=9600)

while True:
    line = ser.readline().decode("utf-8")

Run the program, and it should start receiving numbers from the Pico. Wander around with the Pico powered from a USB battery pack and see how far you can get before you start loosing data. 

pi@pi400:~ $ nano hc_12.py 

pi@pi400:~ $ python3 hc_12.py 








Everything worked without a hitch and no need for any tricky SPI programming or dodgy libraries. Pyserial was all that was required. These are now my wireless modules of choice.

I'm just using the little coiled-wire antenna that came with the module and I'm getting more than enough range to reach the hen house even through a few walls. The modules have a tiny antenna connector, to which an external antenna can be attached for maximum range. The module datasheet claims 1km at maximum power and lowest baud rate. Please check that 433MHz is legal in your area.

Next up for me is using the module's AT commands to balance low power with range and change channel, to avoid interference.

No comments: