0% found this document useful (0 votes)
55 views7 pages

Step-by-Step Guide To Control RRHFOEM02 Over SPI From Raspberry Pi 4

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
55 views7 pages

Step-by-Step Guide To Control RRHFOEM02 Over SPI From Raspberry Pi 4

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

Step-by-Step Guide to Control RRHFOEM02 Over SPI from

Raspberry Pi 4

Step 1: Wiring – Connect Raspberry Pi to RRHFOEM02

Make sure your jumper wires are connected properly between Raspberry
Pi 4 andRRHFOEM02 as per SPI protocol:

Refer to RRHFOEM02 to Raspberry Pi Pin Mapping Document

Step 2: Enable SPI on Raspberry Pi

1. Open terminal on Raspberry Pi:

sudo raspi-config

2. Go to:

o Interface Options → SPI → Select Yes

3. Reboot Raspberry Pi:

sudo reboot

4. Confirm SPI is enabled:

ls /dev/spidev0.*

You should see:

/dev/spidev0.0

/dev/spidev0.1

Step 3: Install Required Python Library

Install spidev for Python:

sudo apt update

sudo apt install python3-pip -y

python3 -m venv ~/myenv

source ~/myenv/bin/activate
pip3 install spidev

Step 4: Write Your Python Code

1. Create a new Python file:

nano rrhf_on_off.py

2. Paste the following example code:

Sample Code 1.

import spidev

import time

# Initialize SPI

spi = spidev.SpiDev()

spi.open(0, 0) # bus 0, CS0

spi.max_speed_hz = 50000

spi.mode = 0b00

# Example command bytes (replace with actual ON/OFF commands for


your module)

ON_COMMAND = [0x01] # Replace with actual "on" command

OFF_COMMAND = [0x00] # Replace with actual "off" command

def send_command(cmd):

response = spi.xfer2(cmd)

print("Sent:", cmd, "| Received:", response)

try:

while True:

print("Sending ON command")
send_command(ON_COMMAND)

time.sleep(2)

print("Sending OFF command")

send_command(OFF_COMMAND)

time.sleep(2)

except KeyboardInterrupt:

print("Exiting...")

finally:

spi.close()

3. Save and exit:

o Press CTRL + X, then Y, then Enter.

Step 5: Run the Code

Run your script using:

 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

1. Create a new Python file:

nano rrhf_on_off2.py

2. Paste the following example code:

import spidev

import time

import RPi.GPIO as GPIO

# SPI settings

SPI_BUS = 0

SPI_DEVICE = 0

SPI_MODE = 3

SPI_SPEED = 250000

# SS pin and IRQ pin

SS_PIN = 8

IRQ_PIN = 17

def spi_init():

"""Initializes SPI and GPIO."""

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

def spi_send_command(spi, command):

"""Sends the command byte array over SPI with manual SS control."""

GPIO.output(SS_PIN, GPIO.LOW) # Select the device (enable)

time.sleep(0.0001) # Short delay for the SS line to stabilize.

spi.xfer2(command)

time.sleep(0.0001) # Short delay.

GPIO.output(SS_PIN, GPIO.HIGH) # Deselect the device (disable) after


receiving the entire response.

def spi_receive_response(spi):

"""Waits for IRQ and receives the response byte by byte with single SS
enable."""

print("Waiting for IRQ...")

GPIO.wait_for_edge(IRQ_PIN, GPIO.RISING)

print("IRQ received. Receiving response byte by byte...")

GPIO.output(SS_PIN, GPIO.LOW) # Select the device (enable)

time.sleep(0.0001) # Short delay for the SS line to stabilize.

received_data = []

while GPIO.input(IRQ_PIN) == GPIO.HIGH:

dummy_data = [0x00]

byte_received = spi.xfer2(dummy_data)[0]

received_data.append(byte_received)

time.sleep(0.0001) # Short delay.


GPIO.output(SS_PIN, GPIO.HIGH) # Deselect the device (disable) after
receiving the entire response.

print("Response received.")

return received_data

def spi_close(spi):

"""Closes SPI and cleans up GPIO."""

spi.close()

GPIO.cleanup()

def main():

"""Main function to send command and receive response."""

try:

spi = spi_init()

# Send the specific command

command = [0x03, 0x2F, 0x01, 0x2D]

spi_send_command(spi, command)

# Receive the response

response = spi_receive_response(spi)

# Convert response to hexadecimal string

hex_string = "".join(f"{byte:02X}" for byte in response)

print(f"Received response (hex): {hex_string}")

except Exception as e:

print(f"An error occurred: {e}")


finally:

if 'spi' in locals():

spi_close(spi)

if __name__ == "__main__":

main()

Run the Code

Run your script using:

 python3 rrhf_on_off2.py

You might also like