Just because the M-Duino line of PLCs don’t have a USB connector available at run-time doesn’t mean you can’t have high-resolution images and video! This article will explain how you can connect a low-cost 5 MP camera to an M-Duino PLC, using the I2C and SPI bus connections.
In addition to the M-Duino PLC, you’ll need a camera and a Windows-based computer with an available USB2 port and the Arduino IDE (I’m using version 1.8.13 and it seems quite stable). There are a number of cameras that are compatible with the M-Duino+ PLCs, but I’ll be using one built by ArduCam based on the OV5642 sensor from OmniVision. The ArduCam camera is the 5.0 MP Mini Module Camera Shield (SKU B0068), available from DLSCorp here; documentation on the ArduCam website can be found here. Don’t hesitate to contact us if you have any questions.
There are eight pins on the camera that need to be connected to the M-Duino:
- VCC – Voltage in (5Vdc)
- GND – Ground
- SCL – I2C bus clock line
- SDA – I2C bus data line
- SCK – SPI bus clock line
- MOSI – SPI bus server output line (client input)
- MISO – SPI bus server input line (client output)
- CS – SPI bus chip select (Arduino pin 3)
All of these lines can be found on two adjacent connectors on the A-zone (Base) of the M-Duino PLC (see section 7 of the User Guide}. The camera from ArduCam includes an 8-line flat ribbon rainbow cable with 1/10″ pins on one end and sockets on the other end, which works well for the Mega2560 board, but no so much for the M-Duino PLC. The connector that comes with the M-Duino doesn’t take the pins (too deep) without some modifications. The least expensive option is to snip off the pins, strip back 1/4″ of the insulation, press the appropriate orange button on the connector, then secure each line in the supplied connector. Another (IMO better) option is to simply remove the black plastic pieces and replace them with 3/4″ or so of 1/16″ heat shrink tubing (be sure to leave 3/8″ of the pin exposed) – this provides a bit of strain relief on the wire. Or, you might want to build your own extender cable so you don’t damage the supplied rainbow cable. In addition to the connections, there are several switches that need to be set – SCL, SDA, and Pin 3 (see section 8 of the User Guide).
Once you have your camera connected, open the Arduino IDE, download ArduCam software from the Arduino repository on the ArduCAM GitHub page. Once you’ve got it, unzip it (if necessary), and read the README.md file. You’ll need to copy the ArduCAM folder to the Arduino libraries folder( probably C:\Program Files (x86)\Arduino\libraries), edit the arduino-1.8.13\ArduCAM\memorysaver.h file (wherever that ended up from your download), and open the sketch by selecting File | Examples | ArduCAM (Examples for any board) | mini. Then, pick the INO file from the appropriate folder: for still images, pick: ArduCAM_Mini_5MP_OV5642_Plus_Functions.ino; for videos, pick ArduCAM_Mini_5MP_OV5642_Plus_Video_Streaming. In both sketches, you’ll need to change where CS is assigned a value (about line 30 or 40) from 7 to 3:
// set pin 3 x7x as the slave select for the digital pot: const int CS = 3; // 7;
Set the Tools | Board to the M-Duino family, and the Tools | Model to your particular model, and set the Tools | Port to the correct USB port. You should be ready to compile and load your sketch. Open the Serial Monitor and set the Baud Rate to 1000000. If things don’t go well, you’ll probably see the following:
ACK CMD ArduCAM Start! END ACK CMD SPI interface Error! END ACK CMD SPI interface Error! END ACK CMD SPI interface Error! END ACK CMD SPI interface Error! END ACK CMD SPI interface Error! END ...
This is probably the result of your pins not being connected properly. Or, possibly the switches not being set properly. Or, possibly the CS value not being set properly. Or, possibly the 24Vdc power supply not being connected or powered on – the Mega2560 will work properly with only the USB power, but the M-Duino requires the 24Vdc in order to work properly. Update: another possibility is a damaged Pin 3 – if this is the case, try Pin 18 or 19. You’ll have to set Pin 2 high, which disables the GPRS function of the PLC!
Or, you might see this:
ACK CMD ArduCAM Start! END ACK CMD SPI interface OK. END ACK CMD Can't find OV5642 module! END ACK CMD Can't find OV5642 module! END ACK CMD Can't find OV5642 module! END ACK CMD Can't find OV5642 module! END ACK CMD Can't find OV5642 module! END ...
This is caused by the wrong camera selected in the memorysaver.h file (or the wrong camera connected to the PLC). Or, you might see this:
ACK CMD ArduCAM Start! END ACK CMD SPI interface OK. END ACK CMD OV5642 detected. END
This is good. You are ready to connect your camera to the host software on your Windows PC. In your ArduCAM/examples/host_app/ArduCAM_Host_V2.0_Windows folder, launch the ArduCAM_Host_V2.exe application. Verify the Port (COMx) and Baudrate (921600) are correct, and click on the Open button. Just to get a quick look to make sure everything is working, click on the Capture button, and you should get a small popup window with an even smaller image displayed.
You should be able to change the resolution – just a heads up – the values in the list don’t match the values you’re selecting. If you select 160×120, you’ll get 320×240. If you pick 1024×768, you’ll get 2592×1944 (which is as high as it goes). You can change Single to Continuous, and it will take pictures repeatedly. There are a few other settings available as well. Select the SaveImage checkbox to save the image (go figure) – it will be in the path specified in the lower left corner. If things seem to get confused, click on Close, and then Open again.