RPi Pico proximity sensor: Ultrasonic and Passive Infrared

Components: DFRobot A01NYUB Waterproof Ultrasonic Sensor and DFRobot PIR (Motion) Sensor (SKU: SEN0171). The circuit board also includes places to put: Arducam, RTC, PiicoDev I2C socket (allow connection of a non coin cell RTC), SD reader, OLED display, rotary encoder and battery supply socket.

The unpopulated board is available for $10 NZ plus postage.

# Import all of the modules that we need
from machine import Pin, UART
from utime import sleep_ms


# Create an LED object that we can turn on and off
avg_pin = machine.Pin(8, machine.Pin.OUT, value=1) # tell sensor to average data in hardware by driving RX pin on sensor high
p0 = Pin(0, Pin.IN)

# Initialise UART
uart = UART(1, baudrate=9600, rx=9)
uart.init(bits=8, parity=None, stop=1) # Defaults from DFrobot wiki page: https://wiki.dfrobot.com/A01NYUB%20Waterproof%20Ultrasonic%20Sensor%20SKU:%20SEN0313


def read_distance():
    uart.read() # clear buffer (could contain many measurements)
    while (uart.any() == 0): # wait for the sensor to send a packet
        pass # pass and check again if you didn't get one
    data_buff = uart.read(4) # read the 4 bytes it sent
    checksum = (data_buff[0] + data_buff[1] + data_buff[2]) & 0xFF # checksum seems to be header + data H + data L, masked to a single byte
    if (checksum == data_buff[3]):
        measurement = ((data_buff[1] << 8) + data_buff[2]) # shift data H left 8 bits, and add with data low
        if measurement == 250:
            return(-1) # return -1 if "250" (invalid measurement) is recieved, consider adding 0 too, as it's usually not accurate
        else:
            return(measurement)
    else:
        return(-2) # if checksum fails (normally because minimum range has been exceeded, return -1)


while True:
    print(str(read_distance()) + " mm, " + str(p0.value())) # convert to string (int can't concatenate) and append mm

=====================================================================

2492 mm, 1
3421 mm, 1
3413 mm, 1
3573 mm, 1
2492 mm, 1
2497 mm, 1
275 mm, 1
-1 mm, 1
-1 mm, 1
-1 mm, 1
-1 mm, 0
-1 mm, 0
-1 mm, 0
-1 mm, 0
-1 mm, 0
274 mm, 0
328 mm, 0
393 mm, 0
430 mm, 0
514 mm, 0
4342 mm, 0
4346 mm, 0
4342 mm, 0
705 mm, 0
4347 mm, 0
4342 mm, 0
4342 mm, 0
4111 mm, 0
2496 mm, 0
823 mm, 0
818 mm, 0
2496 mm, 0
806 mm, 0
4017 mm, 0
3830 mm, 0
2505 mm, 0
678 mm, 0
2492 mm, 0
588 mm, 0
2492 mm, 0
506 mm, 0
488 mm, 0
449 mm, 1
420 mm, 1
386 mm, 1
355 mm, 1
344 mm, 1
-1 mm, 1
2496 mm, 1
2497 mm, 1
2501 mm, 1
2501 mm, 1
1691 mm, 1
2497 mm, 1
-1 mm, 1
314 mm, 1
2488 mm, 1
349 mm, 1
377 mm, 1
3999 mm, 1
2488 mm, 1
2492 mm, 1
3416 mm, 1
2492 mm, 1
2492 mm, 1
2497 mm, 1
2501 mm, 1
2496 mm, 1
2492 mm, 1
2496 mm, 1
2492 mm, 1
2492 mm, 1

RPi Pico proximity sensor: Ultrasonic and Passive Infrared with Arducam Camera

The Arducam (Mega 3MP SPI Camera Module with Camera Case for Any Microcontroller) can connect to this board. I have seen two colour codes so far: VCC, GND, SCK, MISO, MOSI, CS; red, grey, orange , blue, brown, green; red, black, white, yellow, brown, orange. The former was posted on the Arducam website and the later was what the camera actually was, when it arrived. The sample code is here. The camera chip select command needs to be ‘CAM = arducam_mega(7)’ in arducam_mega.py

Added SD reader. Writing jpg images to SD card.

#arducam_mega.py modifications
from machine import Pin, SPI
import time
import sdcard
import uos

...

if __name__ == "__main__":
    CAM = arducam_mega(7)
    CS = const(17)
  
    # Initialize SD card
    sd = sdcard.SDCard(CAM.spi, Pin(CS))
    
    # Mount filesystem
    vfs = uos.VfsFat(sd)
    uos.mount(vfs, "/sd")

    
    #f = open('image.jpg', 'wb')
    CAM.soft_reset()
    CAM.check_connection()
    CAM.set_fmt(MEGA_PIXELFORMAT.JPEG, MEGA_RESOLUTION.VGA) #_96X96)
    CAM.set_exposure(5000)
    CAM.set_exposure_enable(False)
    CAM.set_gain_enable(False)
    CAM.set_gain(100)
    time.sleep_ms(1000)
    frame_len = CAM.capture()
    start_flag = True
    with open("/sd/ardcam01.jpg",'wb') as fsd:
        if frame_len > 0:
            while frame_len > 0 :
                read_len = 1024 if frame_len > 1024 else frame_len
                buf = CAM.read_fifo(read_len,start_flag)
                print(buf)
                #f.write(buf)
                fsd.write(buf)
                frame_len -= len(buf)
                start_flag = False
    f.close()

Sleep 0ms (Note green tinge)

Sleep 500ms (Less green tinge)

Sleep 1000ms (This has the least green tinge)