Software/Arduino Core/Camera

Capture Controls (OV5640)

Exposure, gain, white balance, focus and strobe control on the OV5640, and the interaction between the exposure and gain loops.

intermediateOV56402 min read

This page applies to the OV5640. The VD66GY has no on-sensor control loops; its exposure and white balance are handled by the image signal processor, described in The Global Shutter Camera.

The OV5640 runs three automatic control loops: exposure, gain and white balance. Each re-meters every frame. Classical vision algorithms generally require fixed capture settings, since a colour threshold or a barcode contrast tuned under one metering result changes under the next. The camera object exposes the controls.

Holding the current values

Disabling an automatic loop retains its last converged value rather than resetting to a default:

cpp
camera.setAutoExposure(false);
camera.setAutoGain(false);
camera.setAutoWhiteBalance(false);

The camera is pointed at a representative scene, allowed to settle for about a second, and the loops are then disabled. Subsequent frames are exposed identically.

Explicit values

cpp
camera.setAutoExposure(false);
camera.setExposureUs(4000);      // 4 ms
camera.setAutoGain(false);
camera.setGain(2.0f);            // 2x

Exposure is in microseconds and is clamped to the current mode's frame period. Gain is a multiplier.

Exposure and gain interaction

Automatic exposure controls image brightness towards a target and uses whichever actuator remains enabled. If only exposure is locked, gain compensates and the image returns to the same brightness within a frame. The exposure value is held; the brightness is not. Both are locked for a reproducible image.

The one-sided case has a use: a short fixed exposure with automatic gain freezes motion at the cost of noise.

Methods

MethodFunction
setAutoExposure(bool)Enable or disable automatic exposure. Disabling holds the current value
setAutoGain(bool)Enable or disable automatic gain
setAutoWhiteBalance(bool)Enable or disable automatic white balance
setExposureUs(us)Set exposure time
setGain(x)Set gain
autoFocus(mode)AF_SINGLE, AF_CONTINUOUS or AF_DISABLED
setStrobe(mode)Flash LED synchronised to exposure
controlCaps()Bitmask of supported controls

Methods return 0 on success and a negative value when the sensor does not support the control. They may be called at any time after Vision.begin().

Host controls

With an OV5640 fitted, Neuro Studio shows a Camera Controls panel with the same controls. The board reports its capabilities through controlCaps(); on a sensor without them, such as the VD66GY, the panel is not shown. Host changes take effect immediately and are not persisted. Values that must survive a reboot are placed in the sketch or stored as an asset.

Autofocus

The OV5640 module has a voice coil lens and a focus coprocessor. cfg.af sets the mode at start and camera.autoFocus() changes it later. The first use loads focus firmware into the sensor and takes about one second.

AF_CONTINUOUS refocuses as the scene changes. AF_SINGLE from loop() refocuses on demand. Fine detail such as QR codes, barcodes and AprilTags requires focus; a blurred code does not decode.

Strobe

Carriers with a flash LED connect it to the sensor's strobe pin. STROBE_ENABLED pulses the LED with each exposure. The LED cannot be held on continuously. On a carrier without the LED the call has no effect.

VD66GY

The VD66GY outputs raw Bayer data and has no on-chip automatic loops. Exposure and white balance are handled by the STM32N6's image signal processor, configured through cfg.isp_enabled. The methods on this page return -1 on that sensor. See The Global Shutter Camera.