SR504

Forums:

Hi,

Would someone be able to help as to how to connect the SR504 (https://www.piborg.org/accessories/sr04) to the PiBorg? Also, some sample codes to get it working would be most appreciated.

Thank you.

Best,

Prem

piborg's picture

There are a few ways you can connect the SR04 to a Raspberry Pi to choose from.

We would recommend using one of our UltraBorgs to read the SR04.
This is probably the easiest method as the board does most of the work for you, simply returning actual distance values straight from the library.

The connection and setup for an UltraBorg is all explained here.
The software installation provides a few examples, one of which reads the SR04s to get distances:

#!/usr/bin/env python
# coding: latin-1

# Import the libraries we need
import UltraBorg
import time

# Start the UltraBorg
UB = UltraBorg.UltraBorg()      # Create a new UltraBorg object
UB.Init()                       # Set the board up (checks the board is connected)

# Loop over the sequence until the user presses CTRL+C
print 'Press CTRL+C to finish'
try:
    while True:
        # Read all four ultrasonic values
        usm1 = UB.GetDistance1()
        usm2 = UB.GetDistance2()
        usm3 = UB.GetDistance3()
        usm4 = UB.GetDistance4()
        # Convert to the nearest millimeter
        usm1 = int(usm1)
        usm2 = int(usm2)
        usm3 = int(usm3)
        usm4 = int(usm4)
        # Display the readings
        if usm1 == 0:
            print '#1 No reading'
        else:
            print '#1 % 4d mm' % (usm1)
        if usm2 == 0:
            print '#2 No reading'
        else:
            print '#2 % 4d mm' % (usm2)
        if usm3 == 0:
            print '#3 No reading'
        else:
            print '#3 % 4d mm' % (usm3)
        if usm4 == 0:
            print '#4 No reading'
        else:
            print '#4 % 4d mm' % (usm4)
        print
        # Wait between readings
        time.sleep(.1)
except KeyboardInterrupt:
    # User has pressed CTRL+C
    print 'Done'

Alternatively you can connect the SR04 to the Raspberry Pi itself.
This does not involve any additional boards, but the software is a little more tricky.
There is a good guide here which explains how to do this.
The value you get will be a bit noisy however, the values from UltraBorg are pre-filtered to make them more stable.

Thank you very much! This is very helpful!

Subscribe to Comments for "SR504"