Linksprite serial vga camera working example

Attached is working test software for Linksprite LS-Y201-RS232 using TTL levels to read/communicate with camera.

This software uses Ethernet shield to write to SD card to save file test.txt that needs renamed.

Have used code from various sources and incorporated linksprite jpegcamera library into code so not clean code.

Have disabled Ethernet chip port but may not need to. Surely SD card and Ethernet will work together?

Will try logging to internet site next to do proper logging.

Hope is to log over GPS in long term.

Camera is on ports 8,9 to move from SD on port 4.

Remember to rename test.txt to test.jpg to view.

Camera is set to wait 5 secs before taking picture. Some suggest 10 secs.

Have fun - just thought I would give someone something to start with.

serial_camera_logger.ino (9.05 KB)

Thank you for sharing your work. I would like to know if the camera you are referring to is the one displayed at the following link:
http://store.linksprite.com/infrared-jpeg-color-camera-serial-uart-rs232-level/
Does your's come with the infrared lights?
I have been trying to make my camera work for days, it takes pictures but they are very dark and have a greenish coloration. Can u post a snapshot sample of your camera?

no,

http://store.linksprite.com/infrared-jpeg-color-camera-serial-uart-ttl-level/

used the ttl level camera.

got via ebay
http://www.ebay.co.uk/itm/JPEG-Colour-Camera-TTL-Interface-Infrared-for-Arduino-/111303055029?pt=LH_DefaultDomain_3&hash=item19ea2dd6b5

actual resolution is about 2M not 640x320. 640x320 is default.

have fun

using arduino 1.6 now and needs SPI.h included to compile.

Hey,
Thanks for sharing. I'd like to ask something....
When I connect my camera to 5V it disappears from the "ports" list under tools, the onboard LEDs become dim, there is a sound like if I pulled the arduino plug from my PC and also, the camera becomes pretty hot on the backside right by one of the yellow tantal capacitors (the one to the right top is the cords are pointing upward). When I use 3.3v instead of 5v nothing really happens, and there is just blank spaces printed where there sould be information in the serial.

Does this mean I got a bad module? Want to know as I paid around 45$ for it and don't want to have done so invain! What do you think?

Seems to be a problem. Is is it supposed to be connected to the USB at the same time as 5V? Could you post a picture of your setup?

Hey, thanks for answer Isaac96!!

I'm not sure. As far as i have understood, it is possible to have it connected to the USB at the same time (the example code feature serial.print(); commands, which otherwise would be quite useless. Also they're useing software serial, so not pin 0 and 1 (when I tried those it worked even worse then now). Last time I tried the setup I don't think it became very hot, altough warm which I suppose is normal for a GPU?

The code does create a file on the SD card, but nothing is written to the file (0kb). Also when I have the serial open, the initialization starts fine and then it just writes a massive amount of blank spaces, for several minutes, possibly forver? I haven't had it come to an end yet... As far as I understand, there should be a HEX code where there is now only blank spaces...

This is one of the example codes i've been trying:

//*******************************************************
//              www.linksprite.com
// Note:
// 1. SD must be formated to FAT16
// 2. As the buffer of softserial has 64 bytes,
//    so the code read 32 bytes each time
// 3. Please add the libaray to the lib path
//
// * SD card attached to SPI bus as follows:
// * MOSI - pin 11
// * MISO - pin 12
// * CLK - pin 13
// * CS - pin 4
//*******************************************************
#include <SoftwareSerial.h>
#include <SPI.h>
#include <SD.h>

SoftwareSerial mySerial(10,11);          // Set Arduino pin 4 and 5 as softserial

byte ZERO = 0x00;
byte incomingbyte;
long int j=0,k=0,count=0,i=0x0000;
uint8_t MH,ML;
boolean EndFlag=0;
File  myFile;
int SecondPic=0;
int TakePic=0;
void SendResetCmd()
{
   mySerial.write(0x56);
   mySerial.write(ZERO);
   mySerial.write(0x26);
   mySerial.write(ZERO);
}

/*************************************/
/* Set ImageSize :
/* <1> 0x22 : 160*120
/* <2> 0x11 : 320*240
/* <3> 0x00 : 640*480
/* <4> 0x1D : 800*600
/* <5> 0x1C : 1024*768
/* <6> 0x1B : 1280*960
/* <7> 0x21 : 1600*1200
/************************************/
void SetImageSizeCmd(byte Size)
{
   mySerial.write(0x56);
   mySerial.write(ZERO);  
   mySerial.write(0x54);
   mySerial.write(0x01);
   mySerial.write(Size);
}

/*************************************/
/* Set BaudRate :
/* <1>¡¡0xAE  :   9600
/* <2>¡¡0x2A  :   38400
/* <3>¡¡0x1C  :   57600
/* <4>¡¡0x0D  :   115200
/* <5>¡¡0xAE  :   128000
/* <6>¡¡0x56  :   256000
/*************************************/
void SetBaudRateCmd(byte baudrate)
{
   mySerial.write(0x56);
   mySerial.write(ZERO);
   mySerial.write(0x24);
   mySerial.write(0x03);
   mySerial.write(0x01);
   mySerial.write(baudrate);
}

void SendTakePhotoCmd()
{
   mySerial.write(0x56);
   mySerial.write(ZERO);
   mySerial.write(0x36);
   mySerial.write(0x01);
   mySerial.write(ZERO); 
   i+=0x0000;
}

void SendReadDataCmd()
{
   MH=i/0x100;
   ML=i%0x100;
   mySerial.write(0x56);
   mySerial.write(ZERO);
   mySerial.write(0x32);
   mySerial.write(0x0c);
   mySerial.write(ZERO);
   mySerial.write(0x0a);
   mySerial.write(ZERO);
   mySerial.write(ZERO);
   mySerial.write(MH);
   mySerial.write(ML);
   mySerial.write(ZERO);
   mySerial.write(ZERO);
   mySerial.write(ZERO);
   mySerial.write(0x20);
   mySerial.write(ZERO);
   mySerial.write(0x0a);
   i+=0x20;
}

void StopTakePhotoCmd()
{
   mySerial.write(0x56);
   mySerial.write(ZERO);
   mySerial.write(0x36);
   mySerial.write(0x01);
   mySerial.write(0x03);
   SecondPic = HIGH;
}

void setup()
{
   TakePic=1;
}

void loop()
{
 if (TakePic == 1) {
   CameraSetup();
 }
}

void CameraSetup()
{
 Serial.begin(38400);
 pinMode(53, OUTPUT);  
 while (!Serial)
   Serial.print("Initializing SD card...");
   if (!SD.begin(53))
   {
       Serial.println("initialization failed!");
       return;
   }
   Serial.println("initialization done.");
   Serial.println("please waiting ....");

   mySerial.begin(38400);
   delay(100);
   SendResetCmd();
   delay(2000);
   SetBaudRateCmd(0x2A);
   delay(500);
   SetImageSizeCmd(0x22);
   delay(500);
   mySerial.begin(38400);
   delay(100);
   CameraLoop(); 
}

void CameraLoop ()
{
   byte a[32];
   int ii;
   SendResetCmd();
   delay(2000);                            //Wait 2-3 second to send take picture command
   SendTakePhotoCmd();
   delay(1000);
   while(mySerial.available()>0)
   {
       incomingbyte=mySerial.read();
   }
  if (SecondPic == LOW) {
     myFile = SD.open("pic1.jpg", FILE_WRITE); //The file name should not be too long
  }
  else {
      myFile = SD.open("pic2.jpg", FILE_WRITE); //The file name should not be too long
  }
   while(!EndFlag)
   {
       j=0;
       k=0;
       count=0;
       SendReadDataCmd();
       delay(20);
       while(mySerial.available()>0)
       {
           incomingbyte=mySerial.read();
           k++;
           delay(1); //250 for regular
           if((k>5)&&(j<32)&&(!EndFlag))
           {
               a[j]=incomingbyte;
               if((a[j-1]==0xFF)&&(a[j]==0xD9))     //tell if the picture is finished
               {
                   EndFlag=1;
               }
               j++;
               count++;
           }
       }
       for(j=0;j<count;j++)
       {
           if(a[j]<0x10)  Serial.print("0");
           Serial.print(a[j],HEX);           // observe the image through serial port
           Serial.print(" ");
       }

       for(ii=0; ii<count; ii++)
       myFile.write(a[ii]);
       Serial.println();
   }
   myFile.close();
   Serial.print("Finished writing data to file");
   delay(500);
   StopTakePhotoCmd();
   delay(500);
   SendResetCmd();
   EndFlag=0;
   delay(5000);
   if (SecondPic=HIGH){
       CameraSetup();
       SecondPic=LOW;
       TakePic=0;
   }
   while(1);
}

I don't quite understand how and where in the code i set baudrate and picture size. I can se the codes posted but have no clue where to insert that info? Also this code seems to be for LinkSprite's 2MP camera, so it's not quite the same as mine (640x480) but I think it uses the same processor, LS-Y201. The product number of my version is SEN-10061, it's an older and seemingly outdated model...

It also says the format of the SD card is imporant, and I formatted it as FAT (used to be FAT64) with my computer , but I don't know what type of FAT it is now. It just says FAT, and no number. Must be 32 right, 16 is not commonly used anymore I think?

Do you think a picture is still needed? Currently my arduino is hooked to my main project and I'll get back with a pic as soon as I found the time to tinker some more with it.

We need to see a pic of your setup, yes.

Hey again!
Thanks for the answer, sorry about the delay! Didn't have it assembled and had to find the time to tinker with it some more!

I have got the camera working now, I must have been mixing up tx/rx connections the first time I suppose. Below are some pics of the setup. Sorry about the mess.

I can not get the camera to change resolution, it seems to only take pics in 240x320, even though I change the the code to "SetImageSizeCmd(0x00);" in the second example.

These are two codes that works for taking a pic.

This first one sends a long hex trough the serial window, starting with the jpeg typical "FF D8...". I haven't got it to save the picture to a SD card so it can be viewed, and would like to do this but I can't understand what the variable for the picture is in the code? How can I save it to a SD card (SD.h lib and shield from seeedstudio)?

#include <SoftwareSerial.h>
#include <LSY201.h>

/* assuming the TX and RX pins on the camera are attached to pins 2 and 3 of
 * the arduino. */
SoftwareSerial camera_serial(2, 3);

LSY201 camera;
uint8_t buf[32];

void setup()
{
  Serial.begin(38400);
  camera.setSerial(camera_serial);
  camera_serial.begin(38400);
}

void loop()
{
  Serial.println("Taking picture...");
  camera.takePicture();

  Serial.println("Bytes:");

  uint16_t offset = 0;
  while (camera.readJpegFileContent(offset, buf, sizeof(buf)))
  {
    for (int i = 0; i < sizeof(buf); i ++)
      Serial.print(buf[i], HEX);
    Serial.println(" ");

    offset += sizeof(buf);
  }

  Serial.println("Done.");

  camera.stopTakingPictures();
  delay(10000);
}

This code works, but strangely takes up double sRamof the other..? Is it due to SD logging? Would be nice to have something lighter as the project is already as 63% memory usage.

//*******************************************************
//              www.linksprite.com
// Note:
// 1. SD must be formated to FAT16
// 2. As the buffer of softserial has 64 bytes,
//    so the code read 32 bytes each time
// 3. Please add the libaray to the lib path
//
// * SD card attached to SPI bus as follows:
// * MOSI - pin 11
// * MISO - pin 12
// * CLK - pin 13
// * CS - pin 4
//*******************************************************
#include <SoftwareSerial.h>
#include <SPI.h>
#include <SD.h>
 
SoftwareSerial mySerial(2,3);          // Set Arduino pin 4 and 5 as softserial
 
byte ZERO = 0x00;
byte incomingbyte;
long int j=0,k=0,count=0,i=0x0000;
uint8_t MH,ML;
boolean EndFlag=0;
File  myFile;
int SecondPic=0;
int TakePic=0;
void SendResetCmd()
{
    mySerial.write(0x56);
    mySerial.write(ZERO);
    mySerial.write(0x26);
    mySerial.write(ZERO);
}
 
/*************************************/
/* Set ImageSize :
/* <1> 0x22 : 160*120
/* <2> 0x11 : 320*240
/* <3> 0x00 : 640*480
/* <4> 0x1D : 800*600
/* <5> 0x1C : 1024*768
/* <6> 0x1B : 1280*960
/* <7> 0x21 : 1600*1200
/************************************/
void SetImageSizeCmd(byte Size)
{
    mySerial.write(0x56);
    mySerial.write(ZERO);  
    mySerial.write(0x54);
    mySerial.write(0x01);
    mySerial.write(Size);
}
 
/*************************************/
/* Set BaudRate :
/* <1>¡¡0xAE  :   9600
/* <2>¡¡0x2A  :   38400
/* <3>¡¡0x1C  :   57600
/* <4>¡¡0x0D  :   115200
/* <5>¡¡0xAE  :   128000
/* <6>¡¡0x56  :   256000
/*************************************/
void SetBaudRateCmd(byte baudrate)
{
    mySerial.write(0x56);
    mySerial.write(ZERO);
    mySerial.write(0x24);
    mySerial.write(0x03);
    mySerial.write(0x01);
    mySerial.write(baudrate);
}
 
void SendTakePhotoCmd()
{
    mySerial.write(0x56);
    mySerial.write(ZERO);
    mySerial.write(0x36);
    mySerial.write(0x01);
    mySerial.write(ZERO); 
    i+=0x0000;
}
 
void SendReadDataCmd()
{
    MH=i/0x100;
    ML=i%0x100;
    mySerial.write(0x56);
    mySerial.write(ZERO);
    mySerial.write(0x32);
    mySerial.write(0x0c);
    mySerial.write(ZERO);
    mySerial.write(0x0a);
    mySerial.write(ZERO);
    mySerial.write(ZERO);
    mySerial.write(MH);
    mySerial.write(ML);
    mySerial.write(ZERO);
    mySerial.write(ZERO);
    mySerial.write(ZERO);
    mySerial.write(0x20);
    mySerial.write(ZERO);
    mySerial.write(0x0a);
    i+=0x20;
}
 
void StopTakePhotoCmd()
{
    mySerial.write(0x56);
    mySerial.write(ZERO);
    mySerial.write(0x36);
    mySerial.write(0x01);
    mySerial.write(0x03);
    SecondPic = HIGH;
}
 
void setup()
{
    TakePic=1;
}

void loop()
{
  if (TakePic == 1) {
    CameraSetup();
  }
}

void CameraSetup()
{
  Serial.begin(115200);
  pinMode(10, OUTPUT);  
  while (!Serial)
    Serial.print("Initializing SD card...");
    if (!SD.begin(10))
    {
        Serial.println("initialization failed!");
        return;
    }
    Serial.println("initialization done.");
    Serial.println("please wait ....");
 
    mySerial.begin(115200);
    delay(100);
    SendResetCmd();
    delay(2000);
    SetBaudRateCmd(0x0D); // set baudrate here. 0x0D (115200) works.
    delay(500);
    SetImageSizeCmd(0x11); // set image size here
    delay(500);
    mySerial.begin(38400);
    delay(100);
    CameraLoop(); 
}

void CameraLoop ()
{
    byte a[32];
    int ii;
    SendResetCmd();
    delay(2000);                            //Wait 2-3 second to send take picture command
    SendTakePhotoCmd();
    delay(1000);
    while(mySerial.available()>0)
    {
        incomingbyte=mySerial.read();
    }
   if (SecondPic == LOW) {
      myFile = SD.open("pic1.jpg", FILE_WRITE); //The file name should not be too long
   }
   else {
       myFile = SD.open("pic2.jpg", FILE_WRITE); //The file name should not be too long
   }
    while(!EndFlag)
    {
        j=0;
        k=0;
        count=0;
        SendReadDataCmd();
        delay(20);
        while(mySerial.available()>0)
        {
            incomingbyte=mySerial.read();
            k++;
            delay(1); //250 for regular
            if((k>5)&&(j<32)&&(!EndFlag))
            {
                a[j]=incomingbyte;
                if((a[j-1]==0xFF)&&(a[j]==0xD9))     //tell if the picture is finished
                {
                    EndFlag=1;
                }
                j++;
                count++;
            }
        }
        for(j=0;j<count;j++)
        {
            if(a[j]<0x10)  Serial.print("0");
            Serial.print(a[j],HEX);           // observe the image through serial port
            Serial.print(" ");
        }
 
        for(ii=0; ii<count; ii++)
        myFile.write(a[ii]);
        Serial.println();
    }
    myFile.close();
    Serial.print("Finished writing data to file");
    delay(500);
    StopTakePhotoCmd();
    delay(500);
    SendResetCmd();
    EndFlag=0;
    delay(5000);
    if (SecondPic=HIGH){
        CameraSetup();
        SecondPic=LOW;
        TakePic=0;
    }
    while(1);
}

Here is an example picture from the LSY201 (taken at night). The quality seems to be pretty terrible for a 40$ camera! It will however work for my project.

ut I can't understand what the variable for the picture is in the code?

The "picture" is a number of times filling buf. There is not enough memory on the Arduino to hold the entire picture in memory at one time, so it it ready a small amount at a time.

  while (camera.readJpegFileContent(offset, buf, sizeof(buf)))
  {
    for (int i = 0; i < sizeof(buf); i ++)
      Serial.print(buf[i], HEX);
    Serial.println(" ");

    offset += sizeof(buf);
  }

Change the Serial.print()/println() calls to FILE::write() to write to a file on the SD card.

Hey Gardener

I would like to ask, How did you fix the problem where the code create a file on the SD card , but nothing is written to the file its 0KB and when the serial is open, the initialization starts fine and then it just writes blank spaces?. I'm having the same problem.

al2lo

Would this work for a jpeg ir ttl camera from linksprite:

http://linksprite.com/wiki/index.php5?title=JPEG_Color_Camera_Serial_Interface_with_Built-in_Infrared_(TTL_level)

Because I modified the code a bit. I commented out the:

if (!SD.begin(4)) {
      Serial.println("initialization failed!");
      //return;
    }

return line as you can see just because I dont have a ethernet shield handy. This will let me go through to see what the camera is responding in the next few lines.

Im using Coolterm to view the hex returned but the code stops at Starting 2 and never spits out a response...

This would mean the camera is not responding, right?

Can anyone in here help me with a working sketch for the LS-Y201 IR ttl jpeg camera? I've been unable to get it to work with Arduino UNO.