学习使用串口相机拍摄jpeg并存入SD卡

2016-01-092.4万
AI 快速预览详细 收起
本文介绍了如何使用Arduino UNO、2M_TTL_Camera和SD卡模块,通过串口相机拍摄JPEG照片并存储到SD卡。文章提供了详细的电路连接图和示例代码,包括设置波特率、图像大小等关键参数。适合初学者学习和实践,无需复杂前置技能即可完成。


在网上看到了类似项目,想学习并实现。在这里开一个帖子作为自己的学习记录并和大家交流。

一、资料收集
主要参考LinkSprite上面对于自家相机的使用教程

1.使用器件包括:
  • 1 x Arduino UNO:自己是用的DF家的板子。
  • 1 x 2M_TTL_Camera:买的LinkSprite的,大淘宝上面找的。
  • 学习使用串口相机拍摄jpeg并存入SD卡图2
  • 1 x SD Card Breakout Board买的LinkSprite的,大淘宝上面找的。现在已经知道不需要买他家的,但是刚开始的时候怕器件不同代码不能用啊
  • 学习使用串口相机拍摄jpeg并存入SD卡图3
  • 1 x SD card:准备用自己相机的SD卡。
  • n x 跳线


2.电路连接
学习使用串口相机拍摄jpeg并存入SD卡图1

SD Card Breakout Board:
  • MOSI of SD card breakout  –>  D11 of Arduino
  • MISO of SD card breakout  –>  D12 of Arduino
  • SCK   of SD card breakout –>  D13 of Arduino
  • CS     of SD card breakout –>  D4 of Arduino
  • VCC   of SD card breakout  –>  5v of Arduino

  • 2M_TTL_Camera:
  • RXD of 2MP JPEG camera –> D6 of Arduino
  • TXD of 2MP JPEG camera –> D5 of Arduino
  • VCC of 2MP JPEG camera –> 5v of Arduino
3.示例代码
  1. //*******************************************************
  2. //              www.linksprite.com
  3. // Note:
  4. // 1. SD must be formated to FAT16
  5. // 2. As the buffer of softserial has 64 bytes,
  6. //    so the code read 32 bytes each time
  7. // 3. Please add the libaray to the lib path
  8. //
  9. // * SD card attached to SPI bus as follows:
  10. // * MOSI - pin 11
  11. // * MISO - pin 12
  12. // * CLK - pin 13
  13. // * CS - pin 4
  14. //*******************************************************
  15. #include <SoftwareSerial.h>
  16. #include <SPI.h>
  17. #include <SD.h>
  18. SoftwareSerial mySerial(5,6);          // Set Arduino pin 4 and 5 as softserial
  19. byte ZERO = 0x00;
  20. byte incomingbyte;
  21. long int j=0,k=0,count=0,i=0x0000;
  22. uint8_t MH,ML;
  23. boolean EndFlag=0;
  24. File  myFile;
  25. void SendResetCmd()
  26. {
  27.   mySerial.write(0x56);
  28.   mySerial.write(ZERO);
  29.   mySerial.write(0x26);
  30.   mySerial.write(ZERO);
  31. }
  32. /*************************************/
  33. /* Set ImageSize :
  34. /* <1> 0x22 : 160*120
  35. /* <2> 0x11 : 320*240
  36. /* <3> 0x00 : 640*480
  37. /* <4> 0x1D : 800*600
  38. /* <5> 0x1C : 1024*768
  39. /* <6> 0x1B : 1280*960
  40. /* <7> 0x21 : 1600*1200
  41. /************************************/
  42. void SetImageSizeCmd(byte Size)
  43. {
  44.   mySerial.write(0x56);
  45.   mySerial.write(ZERO);
  46.   mySerial.write(0x54);
  47.   mySerial.write(0x01);
  48.   mySerial.write(Size);
  49. }
  50. /*************************************/
  51. /* Set BaudRate :
  52. /* <1> 0xAE  :   9600
  53. /* <2> 0x2A  :   38400
  54. /* <3> 0x1C  :   57600
  55. /* <4> 0x0D  :   115200
  56. /* <5> 0xAE  :   128000
  57. /* <6> 0x56  :   256000
  58. /*************************************/
  59. void SetBaudRateCmd(byte baudrate)
  60. {
  61.   mySerial.write(0x56);
  62.   mySerial.write(ZERO);
  63.   mySerial.write(0x24);
  64.   mySerial.write(0x03);
  65.   mySerial.write(0x01);
  66.   mySerial.write(baudrate);
  67. }
  68. void SendTakePhotoCmd()
  69. {
  70. mySerial.write(0x56);
  71. mySerial.write(ZERO);
  72. mySerial.write(0x36);
  73. mySerial.write(0x01);
  74. mySerial.write(ZERO);
  75. }
  76. void SendReadDataCmd()
  77. {
  78. MH=i/0x100;
  79. ML=i%0x100;
  80. mySerial.write(0x56);
  81. mySerial.write(ZERO);
  82. mySerial.write(0x32);
  83. mySerial.write(0x0c);
  84. mySerial.write(ZERO);
  85. mySerial.write(0x0a);
  86. mySerial.write(ZERO);
  87. mySerial.write(ZERO);
  88. mySerial.write(MH);
  89. mySerial.write(ML);
  90. mySerial.write(ZERO);
  91. mySerial.write(ZERO);
  92. mySerial.write(ZERO);
  93. mySerial.write(0x20);
  94. mySerial.write(ZERO);
  95. mySerial.write(0x0a);
  96. i+=0x20;
  97. }
  98. void StopTakePhotoCmd()
  99. {
  100. mySerial.write(0x56);
  101. mySerial.write(ZERO);
  102. mySerial.write(0x36);
  103. mySerial.write(0x01);
  104. mySerial.write(0x03);
  105. }
  106. void setup()
  107. {
  108. Serial.begin(115200);
  109. while (!Serial) {
  110. ; // wait for serial port to connect. Needed for Leonardo only
  111. }
  112. Serial.print("Initializing SD card...");
  113. // On the Ethernet Shield, CS is pin 4. It's set as an output by default.
  114. // Note that even if it's not used as the CS pin, the hardware SS pin
  115. // (10 on most Arduino boards, 53 on the Mega) must be left as an output
  116. // or the SD library functions will not work.
  117. if (!SD.begin(4)) {
  118. Serial.println("initialization failed!");
  119. return;
  120. }
  121. Serial.println("initialization done.");
  122. Serial.println("please waiting ....");  
  123. }
  124. void loop()
  125. {
  126.   byte a[32];
  127.   int ii;      
  128.   mySerial.begin(115200);
  129.   delay(200);
  130.   SendResetCmd();//Wait 2-3 second to send take picture command
  131.   delay(2000);
  132.   SetBaudRateCmd(0x2A);
  133.   delay(100);
  134.   mySerial.begin(38400);
  135.   delay(100);
  136.   SetImageSizeCmd(0x21);
  137.   delay(100);
  138.   SendTakePhotoCmd();
  139.   delay(3000);
  140.   while(mySerial.available()>0)
  141.   {
  142.      incomingbyte=mySerial.read();
  143.   }
  144.   myFile = SD.open("pic.jpg", FILE_WRITE); //<strong><span style="color: #ff0000;">The file name should not be too long</span></strong>
  145.   while(!EndFlag)
  146.   {
  147.     j=0;
  148.     k=0;
  149.     count=0;
  150.     SendReadDataCmd();
  151.     delay(5);
  152.     while(mySerial.available()>0)
  153.     {
  154.       incomingbyte=mySerial.read();
  155.       k++;
  156.       delayMicroseconds(100);
  157.       if((k>5)&&(j<32)&&(!EndFlag))
  158.       {
  159.         a[j]=incomingbyte;
  160.         if((a[j-1]==0xFF)&&(a[j]==0xD9))     //tell if the picture is finished
  161.         EndFlag=1;
  162.         j++;
  163.        count++;
  164.       }
  165.      }
  166.     for(j=0;j<count;j++)
  167.     {
  168.       if(a[j]<0x10)  Serial.print("0");
  169.       Serial.print(a[j],HEX);           // observe the image through serial port
  170.       Serial.print(" ");
  171.     }
  172.     for(ii=0; ii<count; ii++)
  173.       myFile.write(a[ii]);
  174.     Serial.println();
  175.   }
  176.   myFile.close();
  177.   Serial.print("Finished writing data to file");
  178.   while(1);
  179. }
复制代码
目前自己对于代码中绝大部分还是一知半解,接下来一段时间一边等待器件到来一边更新自己对于这段代码的学习理解。



二、器件准备
由于是新手,所以找器件还是花了时间,本来傻乎乎都要拜托海淘小能手的GF来帮我代购LinkSprite了,后来终于在淘宝上找到了卖的。这里就不贴连接了免得说广告。话说我是先问了DF家有没有jpeg摄像头,结果木有。(还有淘宝上那家DF官方淘宝,是真的么?)
目前摄像头还没到。


三、学习实践过程
2016年1月10日:学习SD卡模块的线路连接和实例代码。学习资料:
1.本论坛中用户“何处不江南”发布的youtubeSD卡教学视频翻译帖子:https://mc.dfrobot.com.cn/forum.php?mod=viewthread&tid=13198;
2.Arduino的IDE中自带的SD的示例程序;
线路连接:
利用公母头跳线将SD卡模块和Arduino板相连。其中:
VCC连接5V;
GND连接GND;
MOSI连接pin11;
MISO连接pin12;
SCK连接pin13;
SS连接pin4(教程和上面的视图中都是CS,不知为何我的SD卡上面是SS?请大牛指点);
代码编译与运行:
运行了Arduino IDE中自带的SD示例程序
学习使用串口相机拍摄jpeg并存入SD卡图4

一切正常,效果如下图所示:
学习使用串口相机拍摄jpeg并存入SD卡图5

令人惊奇的是,我实现时清空了我的摄影SD卡,然而程序还是识别出来很多历史信息。通过这个例子程序,可以学习到很多查看SD信息和SD卡中文件的方法,有必要学习的还是。

2016年1月11日:串口摄像头到了,晚上开始继续推进进度。
1.串口摄像头带了连接线,连接线是一端用白色塑料和在一起的,另一端的线头是中空的。串口摄像头的针脚插入白色塑料端,连接线的另外一段用公公线头插入。然后按照连接图连线,针脚不够要用到面包板;
2.编译上面查找的代码并上传到Arduino;
3.点击打开串口触发拍照;
4.发现串口出现都是乱码,原来是串口波特率还是9600,看了下代码,因为:
  1. void setup()
  2. {
  3. Serial.begin(115200);
  4. while (!Serial) {
  5. ; // wait for serial port to connect. Needed for Leonardo only
  6. }
复制代码
所以将串口窗口波特率设置为115200,字符恢复;

5.正在等待结果,大量的16进制代码正在刷屏。然而等了好久了。。。6.怀疑是不是因为SD卡的格式不对。因为自己的卡式FAT32,注释里面显示要FAT16。可是格式化中已经没有FAT16,只有个exFAT,尝试后运行输出SD初始化失败。再加上插入SD卡可以看见PIC.JPG文件,只是没有写入数据。故猜测SD卡格式为FAT32应该还是可以的;
7.于是再次运行代码,持续等。这次就让他放着,不管什么时候总有个头吧,先去做其他事了。









创作许可协议

本项目采用 None(不开放任何权利,保留所有权利) 进行许可。

评论(11)
20060606
20060606
我也做过一个,用的是ptc06摄像头,不过失败了
gray6666
gray6666
代码好长,学习了
activemee
activemee
楼主, 后来相机怎么样了?能拍照么? 拍照能用GPRS上传么?
yoyojacky
yoyojacky
准备用来做什么呢?
shen9015
shen9015
向楼主求结果
diaosiki
diaosiki
作者
2016年1月11日更新。
visionsl
visionsl
适合UNO用的TTL摄像头, 能在淘宝找到的都是低像素的, 不知有没800万像素以上的?
diaosiki
diaosiki
作者
回复visionsl
我也抱着同样的问题搜索过,目前没有找到。
diaosiki
diaosiki
作者
2016年1月10日更新。
丄帝De咗臂
丄帝De咗臂
69块钱完全可以接受啊
diaosiki
diaosiki
作者
回复丄帝De咗臂
是的。但是貌似清晰度稍微弱了点。
R2D2
R2D2
DF家有摄像头,https://www.dfrobot.com.cn/goods-752.html
taobao上的dfrobot是真的。
diaosiki
diaosiki
作者
回复R2D2
感谢啊......哎,下次要仔细找。
dsweiliang
dsweiliang
沙发
- 没有更多了 -