Step-by-Step Guide To Control RRHFOEM02 Over SPI From Raspberry Pi 4
Step-by-Step Guide To Control RRHFOEM02 Over SPI From Raspberry Pi 4
Raspberry Pi 4
Make sure your jumper wires are connected properly between Raspberry
Pi 4 andRRHFOEM02 as per SPI protocol:
sudo raspi-config
2. Go to:
sudo reboot
ls /dev/spidev0.*
/dev/spidev0.0
/dev/spidev0.1
source ~/myenv/bin/activate
pip3 install spidev
nano rrhf_on_off.py
Sample Code 1.
import spidev
import time
# Initialize SPI
spi = spidev.SpiDev()
spi.max_speed_hz = 50000
spi.mode = 0b00
def send_command(cmd):
response = spi.xfer2(cmd)
try:
while True:
print("Sending ON command")
send_command(ON_COMMAND)
time.sleep(2)
send_command(OFF_COMMAND)
time.sleep(2)
except KeyboardInterrupt:
print("Exiting...")
finally:
spi.close()
python3 rrhf_on_off2.py
You should see it sending on and off commands every 2 seconds. If the
module responds, you’ll see response bytes in the terminal.
Sample Code 2
nano rrhf_on_off2.py
import spidev
import time
# SPI settings
SPI_BUS = 0
SPI_DEVICE = 0
SPI_MODE = 3
SPI_SPEED = 250000
SS_PIN = 8
IRQ_PIN = 17
def spi_init():
spi = spidev.SpiDev()
spi.open(SPI_BUS, SPI_DEVICE)
spi.mode = SPI_MODE
spi.max_speed_hz = SPI_SPEED
GPIO.setmode(GPIO.BCM)
GPIO.setup(SS_PIN, GPIO.OUT)
GPIO.setup(IRQ_PIN, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
GPIO.output(SS_PIN, GPIO.HIGH)
return spi
"""Sends the command byte array over SPI with manual SS control."""
spi.xfer2(command)
def spi_receive_response(spi):
"""Waits for IRQ and receives the response byte by byte with single SS
enable."""
GPIO.wait_for_edge(IRQ_PIN, GPIO.RISING)
received_data = []
dummy_data = [0x00]
byte_received = spi.xfer2(dummy_data)[0]
received_data.append(byte_received)
print("Response received.")
return received_data
def spi_close(spi):
spi.close()
GPIO.cleanup()
def main():
try:
spi = spi_init()
spi_send_command(spi, command)
response = spi_receive_response(spi)
except Exception as e:
if 'spi' in locals():
spi_close(spi)
if __name__ == "__main__":
main()
python3 rrhf_on_off2.py