A new PicoMite Camera - 2-Megapixel OV2640


Author Message
matherp
Guru

Joined: 11/12/2012
Location: United Kingdom
Posts: 11828
Posted: 05:22pm 24 Jun 2026      

Version 6.03.00RC25 onwards will have support for the OV2640 camera module that is now ubiquitous and a significant update from the OV7670 that has been supported in multiple versions of MMbasic.



The OV2640 support is available on all RP2350 builds except VGA, VGAUSB and HDMIWEB.
For the HDMI and HDMI builds you must be in mode 4 (RGB555) to use the camera. For the PicoMite builds you will need a connected SPI display (with working MISO) and have enough memory free for the firmware to allocate a 153600 byte buffer in main ram (not PSRAM).
The basic resolution used is 320x240 RGB565 - hence the 153600 byte buffer and the firmware supports capture of images at that resolution and an image change algorithm that will detect motion in the camera field.

All good so far BUT the PicoMite OV2640 firmware also supports jpeg capture in resolutions up to 1600x1200. This allows for a trivial program that can monitor the visual fields for movement and then capture the image in much higher resolution.

Here is the manual entry for the new firmware
  Quote  CAMERA OPEN OV2640 PWDNpin, DCLKpin, HSpin,VSpin, RETpin, D0pin
Initialises the OV2640. For HDMI builds the display must be set to mode 4 before executing the command.
The PWDN pin is driven low to power the sensor up.
DCLK is the camera's pixel-clock output.
Enable OPTION SYSTEM I2C and wire SCL and SDA (may be labelled SIOC
and SIOD) with pullups to 3.3V (2K7 recommended). DCLK may be labelled
PCLK, HS may be HREF and VS may be VSYNC on your module.
D0pin is the start of a range of 8 contiguous pins. The OV2640 always uses
320x240 for the colour preview

CAMERA CAPTURE [scale [,x, y]]
Captures the 320x240 RGB565 preview image, exactly as for the OV7670
CAMERA CAPTURE above (scale and x,y position it on an SPI LCD; on the
HDMI versions it is written to the MODE 4 framebuffer). For a higher-quality
full-colour still use CAMERA CAPTURE JPEG.

CAMERA CAPTURE JPEG fname$ [, resolution [,quality]]
Captures a full-colour image using the camera's hardware JPEG engine and
saves it to the file fname$ (.jpg is added if no extension is given).
resolution selects the image size: VGA (640x480, the default), SVGA
(800x600), XGA (1024x768), SXGA (1280x1024) or UXGA (1600x1200 - the
full 2 megapixels).
quality is HIGH, MEDIUM (the default) or LOW, trading file size against
image quality.
During the capture the display framebuffer is used as working memory, so the
screen is disturbed; the next CAMERA CAPTURE restores the preview. The
saved file can be displayed using LOAD JPG.

CAMERA CHANGE image%(), change! [, scale [, x,y]]
Motion detection with the OV2640, working as for the OV7670 CAMERA
CHANGE above but staying in the 320x240 colour preview mode and using
image brightness as the change measure. Size the array DIM
image%(320*240/8-1). A common use is to loop on CAMERA CHANGE and
trigger CAMERA CAPTURE JPEG when change! exceeds a threshold.


There is also a minor enhancement to the LOAD JPG command to allow images to be downscaled to making reviewing easier.
  Quote  
LOAD JPG file$ [, x] [, y] [,mode] [,ximage] [,yimage] [,scale]
Loads a JPG file from ‘fname$’ and writes it to the current output device
(display or framebuffer) starting at ‘x’, ‘y’ (defaults to 0,0).
The optional ‘mode’ parameter specifies if the output is to be dithered.
Bits 0 and 1 specify the output format and bit 2 specifies the type of dithering
to use.
By default mode is set to -1 which indicates no dithering should be applied.
DITHER_FLOYD_STEINBERG_RGB121 0
DITHER_FLOYD_STEINBERG_RGB222 1
DITHER_FLOYD_STEINBERG_RGB332 2
DITHER_ATKINSON_RGB121 4
DITHER_ATKINSON_RGB222 5
DITHER_ATKINSON_RGB332 6
‘ximage’ and ‘yimage’ specify where in the image file to start writing to the
display (defaults to 0,0)
‘scale’ reduces the displayed size of the image by pixel averaging: use 2 for
50% (each output pixel is the average of a 2x2 block of source pixels) or 4 for
25% (a 4x4 block) and 8 for 12.5% (a 8x8 block). The default is 1 which displays the image at full size.
If an extension is not specified “.JPG” will be added to the file name.
Progressive jpg images are not supported.

Taken together, these changes allow a simple motion detection program
Option explicit
Option default none
Dim float ch
Dim imag%(320*240\8-1)
Dim s$
If Instr(MM.DEVICE$,"HDMI") Then Mode 4
Drive "b:"
Camera open ov2640 gp20,gp21,gp22,gp26,gp27,gp0
Do
 Camera change imag%(),ch' initialise the change image
 Do
   Camera change imag%(),ch,1
 Loop Until ch>10
 s$="Movement"
 Inc s$, LEFT$(Date$,2)+Mid$(Date$,4,2)+RIGHT$(Date$,2)
 Inc s$," "+LEFT$(Time$,2)+Mid$(Time$,4,2)+RIGHT$(Time$,2)+".jpg"
 Camera capture jpeg s$,sxga
 Load jpg s$,,,,,4
 Pause 2000
Loop


Here is a sample image taken at 1600x1200 using my HDMIUSB reference board to control the camera