VC0706 で撮影し SDカード に記録する Arduino camera
VC0706シリアルカメラで撮影し、マイクロSDカードに記録するArduino Cameraが出来た。
Arduinoのシールド基板に小さなブレッドボードを貼り付けて、その上に部品を配置してテストした(左の写真)が、Fritzingのブレッドボードで描いてみると右側の絵のようになる。
(注記:カメラの所にある2本の抵抗は、絵では220Ωになっているが、僕がFritzingを使いこなせないために、間違った表記になっている。この抵抗の値は2本とも10kΩである。)
これで撮影した写真は、上下逆転している!
Sketchは、Photo Spy Cam using Arduinoのものが大変参考になった。
各パーツとArduinoとの接続は下記のようになっている。
部品数が少ないので、Arduinoのシールドボードの上にミニブレッドボードを貼り付けて、部品を配置し左表のように結線した。
ここでVC0706は、秋月電子で購入した部品
micro-SDカードは、Amazonで購入
ここでは写真サイズを 640x480 としたが、このサイズにするとSDカードへの記録時間が極端に長くなるので注意を要する。テストの際は、320x240 ぐらいで始めるのが良いのではないだろうか。
.
/**
* VC0706 Camera Module Arduino Compatible:
* + cvbs: N/A
* + 5V: connected to arduino => pin 5V0
* + TX (OUT): connected to the RX (IN) of arduino => pin 2
* + RX (IN): connected to the TX (OUT) of arduino => pin 3
* + GND: connected to the GND
*/
#include <SoftwareSerial.h>
#include <SPI.h>
#include <SD.h>
#include <JPEGCamera.h>
File testFile;
SoftwareSerial s(2, 3);
JPEGCamera cam(s);
char filename[15];
int num = 0;
int numOfBytes;
const int chipSelect = 9; // CS : connected to pin 9 of arduino
int photoSize = 2; // 0 -> 160x120 / 1 -> 320x240 / 2-> 640x480
unsigned long mainStartTime, mainEndTime, startTime, endTime;
void setup()
{
//activate 5V for the SD Card shield
pinMode(8, OUTPUT); // VCC : connected to pin 8 of arduino
digitalWrite(8, HIGH); // SD Card power ON
Serial.begin(38400);
// Wait for the serial port to be opened
while (!Serial) delay(25);
//check SD card
while (!SD.begin(chipSelect))
{
Serial.println("Error: SD initialization failed. Is SD card in the card folder?");
}
Serial.println("Info: SD Initialization complete,");
// Serial port connected to the cam
s.begin(115200); //for VC0706 cam
delay(50);
Serial.println("Info: Reseting cam...");
// reset cam and wait.
cam.reset();
delay(3000);
Serial.println("Info: Changing baud rate...");
// change baud rate to work at 3.3v
cam.chBaudRate(2);// 0 -> 9600 / 1 -> 19200 / 2 -> 38400 / 3 -> 57600 / 4 -> 115200
delay(50);
//change serial to match new camera baud rate
s.end();
s.begin(38400);
delay(50);
Serial.println("Info: Changing picture size...");
//change picture size
cam.chPictureSize(photoSize);
delay(2500);
Serial.println("Info: getting last pic num for the created files...");
num = getNewSDFileNum();
Serial.println("Info: Setup is DONE!");
}
void loop()
{
mainStartTime = millis();
takePic();
mainEndTime = millis();
Serial.print("Info: Main elapsed time (in secs): ");
Serial.println((mainEndTime - mainStartTime) / 1000);
}
void takePic()
{
Serial.println("Info: Preparing to take new photo.");
// must call stopPictures before a new photo is taken.
cam.stopPictures();
delay(50);
// take photo
cam.takePicture();
delay(50);
num++;
sprintf(filename, "PIC_%i.JPG", num);
if (SD.exists(filename)) {
SD.remove(filename);
}
// Create file
testFile = SD.open(filename, FILE_WRITE);
//if it opens ok
if (testFile)
{
//save to file
Serial.print("Info: Saving photo to SD: ");
Serial.println(filename);
startTime = millis();
numOfBytes = cam.readData(testFile);
endTime = millis();
Serial.print("Info: Elapsed time (in secs): ");
Serial.println((endTime - startTime) / 1000);
Serial.print("Info: Bytes for photo: ");
Serial.println(numOfBytes);
}
else //else it didn't open ok
Serial.println("Error: Error opening file on SD,");
//must close file to finish writing.
testFile.close();
}
int getNewSDFileNum() {
File root = SD.open("/");
int n = getFileLastNumForDirectory(root);
root.close();
return n > 0 ? n : 0;
}
int getFileLastNumForDirectory(File dir) {
int n = 0;
int fn = 0;
while (true) {
File entry = dir.openNextFile();
if (!entry) {
// no more files
break;
}
fn = 0;
if (entry.isDirectory()) {
//fn = prepareFileLastNumForDirectory(entry); //no need to be recursively
}
else {
String name = String(entry.name());
if (name.startsWith("PIC_") && name.endsWith(".JPG")) {
int pos = name.indexOf(".JPG");
name = name.substring(4, pos);
if (isValidNumber(name))
fn = name.toInt();
}
}
if (fn > n) {
n = fn;
}
entry.close();
}
return n;
}
boolean isValidNumber(String str) {
if (str && str.length() > 0) {
for (byte i = 0; i < str.length(); i++)
{
if (!isDigit(str.charAt(i))) return false;
}
return true;
}
return false;
}
《撮影した写真》
逆さまに撮影されていた・・・
《参考にしたブログ》
1)Photo Spy Cam using Arduino
このブログは同じVC0706カメラと、同じmicro-SDカードを使った事例だったので、大変勉強になった。
この事例と私の場合のPin接続は違っているのでもし参考にされる場合には、その点を注意してください。
2)オッサンとバイエル、ピアノ等 「カメラで遊んでみる 」
このブログも大変参考にさせて戴いた。
《使用部品》
1)小型TTLシリアルJPEGカメラ VC0706
[adafruit PRODUCT ID: 1386] 秋月電子で購入
2)
« ココログで arduino sketchソースコード を少し見やすくするには・・・ | Main | Fritzing で使うBread board はインスペクターでサイズ変更できる »
「PC and PC troubles」カテゴリの記事
- NodeMCU1.0 or ESP32 Dev Module を使って 8x8 dotmatrix LEDでデジタル時計をつくる(2020.05.21)
- ガラ携Docomo のメールを PCで読む(2019.09.28)
- Linux Mint 19.1 に Virus 対応ソフト ClamAV を Install(2019.05.09)
The comments to this entry are closed.
« ココログで arduino sketchソースコード を少し見やすくするには・・・ | Main | Fritzing で使うBread board はインスペクターでサイズ変更できる »

Comments