Skip to content

Instantly share code, notes, and snippets.

@ferranpons
Last active February 28, 2024 12:03
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ferranpons/faf789e7f69b37997567903037bbcad2 to your computer and use it in GitHub Desktop.
Save ferranpons/faf789e7f69b37997567903037bbcad2 to your computer and use it in GitHub Desktop.
Capture image with Arducam OV5642 + Rpi Pico + SD Card Module + Trigger using CircuitPython
import time as utime
import busio
import board
import usb_cdc
from Arducam import *
from board import *
import os
import sdcardio
import storage
from digitalio import DigitalInOut, Direction, Pull
trigger = DigitalInOut(GP14)
trigger.direction = Direction.INPUT
trigger.pull = Pull.UP
path = '/sd/'
filename = 'test'
extension = '.jpg'
def get_filename():
return path + filename + str(imageCounter) + extension
imageCounter = 0
mode = 0
start_capture = 0
stop_flag=0
once_number=128
value_command=0
flag_command=0
buffer=bytearray(once_number)
isCapturing = False
spi = busio.SPI(GP10, MOSI=GP11, MISO=GP12)
cs = GP13
sd = sdcardio.SDCard(spi, cs)
vfs = storage.VfsFat(sd)
storage.mount(vfs, '/sd')
with open(get_filename(), 'wb'):
pass
mycam = ArducamClass(OV5642)
mycam.Camera_Detection()
mycam.Spi_Test()
mycam.Camera_Init()
utime.sleep(1)
mycam.set_format(JPEG)
mycam.OV5642_set_JPEG_size(OV5642_1280x960)
def get_still(mycam):
once_number = 128
buffer=bytearray(once_number)
count = 0
finished = 0
length = mycam.read_fifo_length()
mycam.SPI_CS_LOW()
mycam.set_fifo_burst()
while finished == 0:
mycam.spi.readinto(buffer, start=0, end=once_number)
print(str(count) + ' of ' + str(length))
with open(get_filename(), 'ab') as fj:
fj.write(buffer)
count += once_number
if count + once_number > length:
print(str(count) + ' of ' + str(length))
count = length - count
mycam.spi.readinto(buffer, start=0, end=count)
with open(get_filename(), 'ab') as fj:
fj.write(buffer)
mycam.SPI_CS_HIGH()
mycam.clear_fifo_flag()
finished = 1
return finished
def capture_image():
mycam.flush_fifo()
mycam.clear_fifo_flag()
mycam.start_capture()
finished = 0
while finished == 0:
if mycam.get_bit(ARDUCHIP_TRIG,CAP_DONE_MASK)!=0:
finished = get_still(mycam)
print('Capture Finished!')
print(get_filename())
while True:
if not trigger.value:
print("Triggered")
if not isCapturing:
isCapturing = True
capture_image();
imageCounter = imageCounter + 1
else:
isCapturing = False
pass
utime.sleep(0.1)
@shepai
Copy link

shepai commented May 12, 2023

Really useful - thank you so much for making this code available! I was wondering if you knew of a way to open the image saved on the sd and hold it as pixel values of shape (height,width,3) in a ulab.numpy array?

@ferranpons
Copy link
Author

@shepai I don't have the code but maybe you could use the Pillow Library for doing that.

@Muhammad-Hazimi-Yusri
Copy link

Muhammad-Hazimi-Yusri commented Feb 13, 2024

Thank you very much for the code for reference, I experimented around with it a bit and found that increasing the once_number value (which I understand is the buffer size?) helps in reducing the time taken for the picture to finish taken and saved on sd card, is there a reason why its not set higher by default, and what does this buffer size thing really means?

It is unfortunate that it took so long to take a single picture as I had assumed it is viable to use this hardware for video related project but this proven to be wrong after delving more into the hardware (OV5642) and github issues on related codebase including Arduino one as I recently posted in my own issue post ArduCAM/PICO_SPI_CAM#14. In this regard, have u done any testing regarding the hardware video capabilities especially when used together with Raspberry Pi Pico (RP2040). Thanks again.

Best regards,
Muhammad

@ferranpons
Copy link
Author

Hi Muhammad!

what does this buffer size thing really means?

This was picked from another script I found but did not experiment with it any further because I had so many issues making it work.

have u done any testing regarding the hardware video

No, I was only looking to take pictures. It's possible to record a video but I cannot help you with that.

Unfortunately, Arducam did not put any effort into making the software accessible to all consumers. They only focus on hardware, and that is an error. I'm sorry I can't help you. I hope you have better luck than me with contacting Arducam because they usually don't answer any questions on their GitHub repositories. 😞

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment