7 May 2015

CH376S USB Read/Write module

Have you ever wondered if there was a way to store and retrieve data from a USB stick with an Arduino UNO? Most people choose SD cards to store their project data, but you may be surprised there IS a way!
IC Station have a nice little module which allows you store and retrieve your Arduino (or other MCU) project data to a USB stick.
 
I am not too sure why USB storage is not widely used in Arduino projects? These modules are not expensive, they have been around for quite a while, and are relatively simple to use. You do not need any libraries to get them to work, however, I must say that documentation for this module is not that easy to find. This site and this document proved to be very useful in my endevour to get this module working, and I hope my tutorial below will help you get started and bridge some of the information gaps.
 
The "CH376S USB read/write module" has a CH376S chip onboard which does most of the hard work for you. All you have to do is send the module some commands from the Arduino and the CH376S chip will do the rest. You can communicate with the module in three different ways:

  • Parallel communication
  • SPI communication
  • and Serial (UART) communication.

This project will show you the connections and code for the Serial (UART) communication method only.


 

Parts Required:

Remove the Jumper

When the CH376S USB module arrives in it's package, it will have a jumper between the TXD pin and GND. You will need to remove this jumper to make the necessary connections between the Arduino UNO and the CH376S USB module.


 

Fritzing Sketch

Please note, that the Arduino Sketch makes use of the Arduino UNO's onboard LED on digital pin 13. The Fritzing sketch below shows an LED + 300 ohm resistor on a breadboard. This is optional. The LED is not a necessary component of CH376S module communication.

Also be aware that the CH376S USB module has an onboard LED just above the TXD and GND pins near the USB port. This LED will only turn on providing the CH376S module is in USB mode AND a USB device has been inserted into the USB port. Both conditions must be met before the module's onboard LED will illuminate. You will not see the LED turn on just by powering the board.
 
The wire diagram below is the correct setup for Serial communication between an Arduino UNO and the CH376S module. If you wish to use SPI or Parallel communication, you will need to refer to the datasheet.


 
 

Arduino Sketch


 
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
/* ===============================================================
      Project: CH376S USB Read/Write Module testing ground
       Author: Scott C
      Created: 1st May 2015
  Arduino IDE: 1.6.2
      Website: http://arduinobasics.blogspot.com/p/arduino-basics-projects-page.html
  Description: This project will allow you to perform many of the functions available on the CH376S module.
               Checking connection to the module, putting the module into USB mode, resetting the module, 
               reading, writing, appending text to files on the USB stick. This is very useful alternative to
               SD card modules, plus it doesn't need any libraries.
================================================================== */

#include <SoftwareSerial.h>

byte computerByte;           //used to store data coming from the computer
byte USB_Byte;               //used to store data coming from the USB stick
int LED = 13;                //the LED is connected to digital pin 13 
int timeOut = 2000;          //TimeOut is 2 seconds. This is the amount of time you wish to wait for a response from the CH376S module.
String wrData = "What is the meaning of life ?";     //We will write this data to a newly created file.
String wrData2 = "42";                                   //We will append this data to an already existing file.

SoftwareSerial USB(10, 11);                           // Digital pin 10 on Arduino (RX) connects to TXD on the CH376S module
                                                      // Digital pin 11 on Arduino (TX) connects to RXD on the CH376S module
                                                      // GND on Arduino to GND on CH376S module
                                                      // 5V on Arduino to 5V on CH376S module
//==============================================================================================================================================
void setup() {
  Serial.begin(9600);                                 // Setup serial communication with the computer (using a baud rate of 9600 on serial monitor)
  USB.begin(9600);                                    // Setup serial communication with the CH376S module (using the default baud rate of 9600)
  pinMode(LED,OUTPUT);                                // Define digital pin 13 as an OUTPUT pin - so that we can use it with an LED
  digitalWrite(LED,LOW);                              // Turn off the LED
}

//================================================================================================================================================
void loop() {
  if(Serial.available()){
    computerByte = Serial.read();                      //read any incoming bytes from the Serial monitor, and store this byte in the variable called computerByte
    if(computerByte==49){               //1            //If you send the number 1 from the serial monitor, the arduino will read it as digital number 49. Google "ascii table" for more info.
      printCommandHeader("COMMAND1: CHECK CONNECTION");
      checkConnection(0x01);                           // Check for successful connection and communication with the CH376S module.
    } 
    if(computerByte==50){               //2
     printCommandHeader("COMMAND2: set_USB_Mode");
      set_USB_Mode(0x06);                              // Code used to enable read/write communication and monitoring of the USB stick
    }
    if(computerByte==51){               //3
      printCommandHeader("COMMAND3: resetALL");
      resetALL();                                      // Reset the USB device
    }
    if(computerByte==52){               //4
      printCommandHeader("COMMAND4: Create and Write to File : TEST4.TXT");
      writeFile("TEST4.TXT", wrData);                  // Create a file called TEST4.TXT and then Write the contents of wrData to this file
    }
    if(computerByte==53){               //5
      printCommandHeader("COMMAND5: Read File: TEST4.TXT");
      readFile("TEST4.TXT");                           // Read the contents of this file on the USB disk, and display contents in the Serial Monitor
    }
    if(computerByte==54){               //6
      printCommandHeader("COMMAND6: Append data to file: TEST4.TXT");
      appendFile("TEST4.TXT", wrData2);                // Append data to the end of the file.
    }
    if(computerByte==55){               //7
      printCommandHeader("COMMAND7: Delete File: TEST4.TXT");
      fileDelete("TEST4.TXT");                         // Delete the file named TEST4.TXT
    }
    if(computerByte==56){               //8
      printCommandHeader("COMMAND8: Read File: TEST2.TXT");
      readFile("TEST2.TXT");                           // Read the contents of the TEST2.TXT file on the USB disk, and display contents in the Serial Monitor
    }
    if(computerByte==57){               //9
      printCommandHeader("COMMAND9: Read File: TEST3.TXT");
      readFile("TEST3.TXT");                           // Read the contents of the TEST3.TXT file on the USB disk, and display contents in the Serial Monitor
    }
  }
  
  if(USB.available()){                                 // This is here to capture any unexpected data transmitted by the CH376S module
    Serial.print("CH376S has just sent this code:");
    Serial.println(USB.read(), HEX);
  }
}

//END OF LOOP FUNCTION ========================================================================================================================================

//print Command header
void printCommandHeader(String header){
   Serial.println("======================");
   Serial.println("");
   Serial.println(header);
   Serial.println("----------------------");
}

//checkConnection==================================================================================
//This function is used to check for successful communication with the CH376S module. This is not dependant of the presence of a USB stick.
//Send any value between 0 to 255, and the CH376S module will return a number = 255 - value. 
void checkConnection(byte value){
  USB.write(0x57);
  USB.write(0xAB);
  USB.write(0x06);
  USB.write(value);
  
  if(waitForResponse("checking connection")){       //wait for a response from the CH376S. If CH376S responds, it will be true. If it times out, it will be false.
    if(getResponseFromUSB()==(255-value)){
       Serial.println(">Connection to CH376S was successful.");
       blinkLED();                               //blink the LED for 1 second if the connection was successful
    } else {
      Serial.print(">Connection to CH376S - FAILED.");
    }
  }
}

//set_USB_Mode=====================================================================================
//Make sure that the USB is inserted when using 0x06 as the value in this specific code sequence
void set_USB_Mode (byte value){
  USB.write(0x57);
  USB.write(0xAB);
  USB.write(0x15);
  USB.write(value);
  
  delay(20);
  
  if(USB.available()){
    USB_Byte=USB.read();
    //Check to see if the command has been successfully transmitted and acknowledged.
    if(USB_Byte==0x51){                                   // If true - the CH376S has acknowledged the command.
        Serial.println("set_USB_Mode command acknowledged"); //The CH376S will now check and monitor the USB port
        USB_Byte = USB.read();
        
        //Check to see if the USB stick is connected or not.
        if(USB_Byte==0x15){                               // If true - there is a USB stick connected
          Serial.println("USB is present");
          blinkLED();                                     // If the process was successful, then turn the LED on for 1 second 
        } else {
          Serial.print("USB Not present. Error code:");   // If the USB is not connected - it should return an Error code = FFH
          Serial.print(USB_Byte, HEX);
          Serial.println("H");
        }
        
    } else {
        Serial.print("CH3765 error!   Error code:");
        Serial.print(USB_Byte, HEX);
        Serial.println("H");
    }   
  }
  delay(20);
}

//resetALL=========================================================================================
//This will perform a hardware reset of the CH376S module - which usually takes about 35 msecs =====
void resetALL(){
    USB.write(0x57);
    USB.write(0xAB);
    USB.write(0x05);
    Serial.println("The CH376S module has been reset !");
    delay(200);
}

//readFile=====================================================================================
//This will send a series of commands to read data from a specific file (defined by fileName)
void readFile(String fileName){
  resetALL();                     //Reset the module
  set_USB_Mode(0x06);             //Set to USB Mode
  diskConnectionStatus();         //Check that communication with the USB device is possible
  USBdiskMount();                 //Prepare the USB for reading/writing - you need to mount the USB disk for proper read/write operations.
  setFileName(fileName);          //Set File name
  fileOpen();                     //Open the file for reading
  int fs = getFileSize();         //Get the size of the file
  fileRead();                     //***** Send the command to read the file ***
  fileClose(0x00);                //Close the file
}

//writeFile========================================================================================
//is used to create a new file and then write data to that file. "fileName" is a variable used to hold the name of the file (e.g TEST.TXT). "data" should not be greater than 255 bytes long. 
void writeFile(String fileName, String data){
  resetALL();                     //Reset the module
  set_USB_Mode(0x06);             //Set to USB Mode
  diskConnectionStatus();         //Check that communication with the USB device is possible
  USBdiskMount();                 //Prepare the USB for reading/writing - you need to mount the USB disk for proper read/write operations.
  setFileName(fileName);          //Set File name
  if(fileCreate()){               //Try to create a new file. If file creation is successful
    fileWrite(data);              //write data to the file.
  } else {
    Serial.println("File could not be created, or it already exists");
  }
  fileClose(0x01);
}

//appendFile()====================================================================================
//is used to write data to the end of the file, without erasing the contents of the file.
void appendFile(String fileName, String data){
    resetALL();                     //Reset the module
    set_USB_Mode(0x06);             //Set to USB Mode
    diskConnectionStatus();         //Check that communication with the USB device is possible
    USBdiskMount();                 //Prepare the USB for reading/writing - you need to mount the USB disk for proper read/write operations.
    setFileName(fileName);          //Set File name
    fileOpen();                     //Open the file
    filePointer(false);             //filePointer(false) is to set the pointer at the end of the file.  filePointer(true) will set the pointer to the beginning.
    fileWrite(data);                //Write data to the end of the file
    fileClose(0x01);                //Close the file using 0x01 - which means to update the size of the file on close. 
}
  
//setFileName======================================================================================
//This sets the name of the file to work with
void setFileName(String fileName){
  Serial.print("Setting filename to:");
  Serial.println(fileName);
  USB.write(0x57);
  USB.write(0xAB);
  USB.write(0x2F);
  USB.write(0x2F);         // Every filename must have this byte to indicate the start of the file name.
  USB.print(fileName);     // "fileName" is a variable that holds the name of the file.  eg. TEST.TXT
  USB.write((byte)0x00);   // you need to cast as a byte - otherwise it will not compile.  The null byte indicates the end of the file name.
  delay(20);
}

//diskConnectionStatus================================================================================
//Check the disk connection status
void diskConnectionStatus(){
  Serial.println("Checking USB disk connection status");
  USB.write(0x57);
  USB.write(0xAB);
  USB.write(0x30);

  if(waitForResponse("Connecting to USB disk")){       //wait for a response from the CH376S. If CH376S responds, it will be true. If it times out, it will be false.
    if(getResponseFromUSB()==0x14){               //CH376S will send 0x14 if this command was successful
       Serial.println(">Connection to USB OK");
    } else {
      Serial.print(">Connection to USB - FAILED.");
    }
  }
}

//USBdiskMount========================================================================================
//initialise the USB disk and check that it is ready - this process is required if you want to find the manufacturing information of the USB disk
void USBdiskMount(){
  Serial.println("Mounting USB disk");
  USB.write(0x57);
  USB.write(0xAB);
  USB.write(0x31);

  if(waitForResponse("mounting USB disk")){       //wait for a response from the CH376S. If CH376S responds, it will be true. If it times out, it will be false.
    if(getResponseFromUSB()==0x14){               //CH376S will send 0x14 if this command was successful
       Serial.println(">USB Mounted - OK");
    } else {
      Serial.print(">Failed to Mount USB disk.");
    }
  }
}

//fileOpen========================================================================================
//opens the file for reading or writing
void fileOpen(){
  Serial.println("Opening file.");
  USB.write(0x57);
  USB.write(0xAB);
  USB.write(0x32);
  if(waitForResponse("file Open")){                 //wait for a response from the CH376S. If CH376S responds, it will be true. If it times out, it will be false.
    if(getResponseFromUSB()==0x14){                 //CH376S will send 0x14 if this command was successful  
       Serial.println(">File opened successfully.");
    } else {
      Serial.print(">Failed to open file.");
    }
  }
}

//setByteRead=====================================================================================
//This function is required if you want to read data from the file. 
boolean setByteRead(byte numBytes){
  boolean bytesToRead=false;
  int timeCounter = 0;
  USB.write(0x57);
  USB.write(0xAB);
  USB.write(0x3A);
  USB.write((byte)numBytes);   //tells the CH376S how many bytes to read at a time
  USB.write((byte)0x00);
  if(waitForResponse("setByteRead")){       //wait for a response from the CH376S. If CH376S responds, it will be true. If it times out, it will be false.
    if(getResponseFromUSB()==0x1D){         //read the CH376S message. If equal to 0x1D, data is present, so return true. Will return 0x14 if no data is present.
      bytesToRead=true;
    }
  }
  return(bytesToRead);
} 

//getFileSize()===================================================================================
//writes the file size to the serial Monitor.
int getFileSize(){
  int fileSize=0;
  Serial.println("Getting File Size");
  USB.write(0x57);
  USB.write(0xAB);
  USB.write(0x0C);
  USB.write(0x68);
  delay(100);
  Serial.print("FileSize =");
  if(USB.available()){
    fileSize = fileSize + USB.read();
  } 
  if(USB.available()){
    fileSize = fileSize + (USB.read()*255);
  } 
  if(USB.available()){
    fileSize = fileSize + (USB.read()*255*255);
  } 
  if(USB.available()){
    fileSize = fileSize + (USB.read()*255*255*255);
  }     
  Serial.println(fileSize);
  delay(10);
  return(fileSize);
}


//fileRead========================================================================================
//read the contents of the file
void fileRead(){
  Serial.println("Reading file:");
  byte firstByte = 0x00;                     //Variable to hold the firstByte from every transmission.  Can be used as a checkSum if required.
  byte numBytes = 0x40;                      //The maximum value is 0x40  =  64 bytes
 
  while(setByteRead(numBytes)){              //This tells the CH376S module how many bytes to read on the next reading step. In this example, we will read 0x10 bytes at a time. Returns true if there are bytes to read, false if there are no more bytes to read.
    USB.write(0x57);
    USB.write(0xAB);
    USB.write(0x27);                          //Command to read ALL of the bytes (allocated by setByteRead(x))
    if(waitForResponse("reading data")){      //Wait for the CH376S module to return data. TimeOut will return false. If data is being transmitted, it will return true.
        firstByte=USB.read();                 //Read the first byte
        while(USB.available()){
          Serial.write(USB.read());           //Send the data from the USB disk to the Serial monitor
          delay(1);                           //This delay is necessary for successful Serial transmission
        }
    }
    if(!continueRead()){                       //prepares the module for further reading. If false, stop reading.
      break;                                   //You need the continueRead() method if the data to be read from the USB device is greater than numBytes.
    }
  }
  Serial.println();
  Serial.println("NO MORE DATA");
}

//fileWrite=======================================================================================
//are the commands used to write to the file
void fileWrite(String data){
  Serial.println("Writing to file:");
  byte dataLength = (byte) data.length();         // This variable holds the length of the data to be written (in bytes)
  Serial.println(data);
  Serial.print("Data Length:");
  Serial.println(dataLength);
  delay(100);
  // This set of commands tells the CH376S module how many bytes to expect from the Arduino.  (defined by the "dataLength" variable)
  USB.write(0x57);
  USB.write(0xAB);
  USB.write(0x3C);
  USB.write((byte) dataLength);
  USB.write((byte) 0x00);
  if(waitForResponse("setting data Length")){      // Wait for an acknowledgement from the CH376S module before trying to send data to it
    if(getResponseFromUSB()==0x1E){                // 0x1E indicates that the USB device is in write mode.
      USB.write(0x57);
      USB.write(0xAB);
      USB.write(0x2D);
      USB.print(data);                             // write the data to the file
  
      if(waitForResponse("writing data to file")){   // wait for an acknowledgement from the CH376S module
      }
      Serial.print("Write code (normally FF and 14): ");
      Serial.print(USB.read(),HEX);                // code is normally 0xFF
      Serial.print(",");
      USB.write(0x57);
      USB.write(0xAB);
      USB.write(0x3D);                             // This is used to update the file size. Not sure if this is necessary for successful writing.
      if(waitForResponse("updating file size")){   // wait for an acknowledgement from the CH376S module
      }
      Serial.println(USB.read(),HEX);              //code is normally 0x14
    }
  }
}

//continueRead()==================================================================================
//continue to read the file : I could not get this function to work as intended.
boolean continueRead(){
  boolean readAgain = false;
  USB.write(0x57);
  USB.write(0xAB);
  USB.write(0x3B);
  if(waitForResponse("continueRead")){       //wait for a response from the CH376S. If CH376S responds, it will be true. If it times out, it will be false.
     if(getResponseFromUSB()==0x14){         //CH376S will send 0x14 if this command was successful
       readAgain=true;
     }
  }
  return(readAgain);
} 

//fileCreate()========================================================================================
//the command sequence to create a file
boolean fileCreate(){
  boolean createdFile = false;
  USB.write(0x57);
  USB.write(0xAB);
  USB.write(0x34);
  if(waitForResponse("creating file")){       //wait for a response from the CH376S. If file has been created successfully, it will return true.
     if(getResponseFromUSB()==0x14){          //CH376S will send 0x14 if this command was successful
       createdFile=true;
     }
  }
  return(createdFile);
}


//fileDelete()========================================================================================
//the command sequence to delete a file
void fileDelete(String fileName){
  setFileName(fileName);
  delay(20);
  USB.write(0x57);
  USB.write(0xAB);
  USB.write(0x35);
  if(waitForResponse("deleting file")){       //wait for a response from the CH376S. If file has been created successfully, it will return true.
     if(getResponseFromUSB()==0x14){          //CH376S will send 0x14 if this command was successful
       Serial.println("Successfully deleted file");
     }
  }
}
  

//filePointer========================================================================================
//is used to set the file pointer position. true for beginning of file, false for the end of the file.
void filePointer(boolean fileBeginning){
  USB.write(0x57);
  USB.write(0xAB);
  USB.write(0x39);
  if(fileBeginning){
    USB.write((byte)0x00);             //beginning of file
    USB.write((byte)0x00);
    USB.write((byte)0x00);
    USB.write((byte)0x00);
  } else {
    USB.write((byte)0xFF);             //end of file
    USB.write((byte)0xFF);
    USB.write((byte)0xFF);
    USB.write((byte)0xFF);
  }
  if(waitForResponse("setting file pointer")){       //wait for a response from the CH376S. 
     if(getResponseFromUSB()==0x14){                 //CH376S will send 0x14 if this command was successful
       Serial.println("Pointer successfully applied");
     }
  }
}


//fileClose=======================================================================================
//closes the file
void fileClose(byte closeCmd){
  Serial.println("Closing file:");
  USB.write(0x57);
  USB.write(0xAB);
  USB.write(0x36);
  USB.write((byte)closeCmd);                                // closeCmd = 0x00 = close without updating file Size, 0x01 = close and update file Size

  if(waitForResponse("closing file")){                      // wait for a response from the CH376S. 
     byte resp = getResponseFromUSB();
     if(resp==0x14){                                        // CH376S will send 0x14 if this command was successful
       Serial.println(">File closed successfully.");
     } else {
       Serial.print(">Failed to close file. Error code:");
       Serial.println(resp, HEX);
     }  
  }
}

//waitForResponse===================================================================================
//is used to wait for a response from USB. Returns true when bytes become available, false if it times out.
boolean waitForResponse(String errorMsg){
  boolean bytesAvailable = true;
  int counter=0;
  while(!USB.available()){     //wait for CH376S to verify command
    delay(1);
    counter++;
    if(counter>timeOut){
      Serial.print("TimeOut waiting for response: Error while: ");
      Serial.println(errorMsg);
      bytesAvailable = false;
      break;
    }
  }
  delay(1);
  return(bytesAvailable);
}

//getResponseFromUSB================================================================================
//is used to get any error codes or messages from the CH376S module (in response to certain commands)
byte getResponseFromUSB(){
  byte response = byte(0x00);
  if (USB.available()){
    response = USB.read();
  }
  return(response);
}



//blinkLED==========================================================================================
//Turn an LED on for 1 second
void blinkLED(){
  digitalWrite(LED, HIGH);
  delay(1000);
  digitalWrite(LED,LOW);
}


If you copy and paste this code directly into the Arduino IDE; you may get a warning like this when you compile the code:
 
   "Low memory available, stability problems may occur".
 
I managed to run the sketch without any issues, however, I did experience problems with some of the methods when I had made further memory hungry modifications. If you do encounter problems, I would recommend that you eliminate any methods which you do not plan to use, and perhaps reduce the number of Serial.print statements throughout the code. However, please note that some of the methods will not work unless the module is in the correct state, so be careful which methods you delete. For example, I found that I could get some simple functionality without the "USBdiskMount()" method. However, I could not read/write data beyond a certain length without this method.
 
Also please note, that some of the methods called within the reading and writing sequence do not need to be called every time. They can be called once in setup, while other methods within the sequence will need to be called every time. I grouped them all together for simplicity.


Serial Commands

Have a look at the following presentation for a summary of the Serial commands used in this tutorial:
 


 
 



If you like this page, please do me a favour and show your appreciation :

 
Visit my ArduinoBasics Google + page.
Follow me on Twitter by looking for ScottC @ArduinoBasics.
I can also be found on Pinterest and Instagram.
Have a look at my videos on my YouTube channel.




However, if you do not have a google profile...
Feel free to share this page with your friends in any way you see fit.

67 comments:

  1. Sir is it possible to copy a file from 1 flash drive to another using two ch376s and arduino atmega 2560? Thanks. I hope for your response

    ReplyDelete
    Replies
    1. Hi Cris,
      I guess it is possible, but not sure why you wouldn't just use a computer ?
      I have not tested this with anything but "*.txt" files. I don't know if you can copy any file type??
      There isn't much information about this module on the internet at the moment, so sorry for my vague answers... if I get a chance, I might try this, but it might be a while until I get round to it.

      Delete
  2. Because i wish to make a portable file transfer device for a project using two of ths chips.
    Thanks anyway! :)

    ReplyDelete
    Replies
    1. That makes sense Cris. What kind of files?

      Delete
    2. All types of files.or at least ms offi e type of files.i have ch375b right now.

      Delete
    3. Did you get it to work ?

      Delete
    4. You don't need anything special for this, just two modules and a MCU with two UARTS.
      Just read one file bytes and send them to write to the other module.

      Delete
  3. Nice Job. Thank you
    I have a question.
    It's possible to write a file to usb key without user interaction.... for example.
    Starting without an usb inside the module, i put in the usb disk and automatically the code inside arduino intercept the event and write the file.
    Thanks

    Luca

    ReplyDelete
    Replies
    1. Hi Luca,
      Once you put the module into USB mode (i.e Command Sequence #2),
      you can see from the video around (3:12), that the module will monitor the USB port.
      So when you insert a USB drive into the module, it will send (0x15) to the Arduino.
      It will also notify the Arduino when the USB is removed by sending this byte code (0x16)

      Here is the link to the relevant part of the video which shows this in action:
      VIDEO LINK

      Once you receive this command within the Arduino, you can then get it to do whatever you need it to.

      Delete
  4. Hi
    i want to use a barcode scanner to get the barcode number and use that number to open an image with the same name. is this doable?

    if so how?

    ReplyDelete
    Replies
    1. Hi Anonymous,
      I have not tried to open an image with this module, but I assume that it is possible, providing you have the necessary hardware to display the image. There are so many ways you could tackle this project. Is it doable? I guess yes, but it depends on how you want to do it. The things you would have to think about are:
      1. Is this a portable project ? Connected or not connected to a computer?
      2. How do you plan to display the image?
      3. What is the size and format of the image?
      4. The format of the barcode? Can you decipher it?
      5. What modules you have or plan to buy?
      6. The memory limitations of the Arduino and which Arduino you plan to use?
      7. Powering the project (including the barcode reader)

      How you would do it , and whether it is possible will depend on your specific project requirements.

      Delete
  5. I have been looking at this project with interest. I would like to create a portable backup device for a digital camera. Connect the camera to one of the USB boards and a large USB pen in the second USB board.
    Anybody done anything like this and would like to share experiences.

    ReplyDelete
    Replies
    1. That would be turtle slow! We are talking about 9600 baud rate! That is sending a picture via IR port, very slow IR port.

      Delete
  6. Hello sir :) great tutorial...Is it possible to view directories inside the flash drive?Can you give me some suggestion if there is any chip that can accomplish this task as well???

    ReplyDelete
    Replies
    1. Hi vindude,
      It may be possible, but I have not tried it.
      It probably will not be straight forward.. and would probably need a bit of programming to accomplish. The only commands available to you are within this document:
      http://www.mpja.com/download/ch376ds1.pdf

      As for any other chips that are capable - I am not sure either.
      Sorry.

      Delete
    2. yes i have seen document and couldn't find any thing of help,,,if you could come up of something please inform :) thank you

      Delete
    3. Sure - I will let you know if I ever come across the solution (if there is any).

      Delete
  7. Can I power this with 3.3 volts? do you know if i could hook this up to an ESP8266 to make it like a WiFi USB drive?
    thanks

    ReplyDelete
    Replies
    1. According to the ICStation CH376S U Disk Read Write Module webpage - you can power it with 3.3V. I guess you could make like a WiFi USB drive - but access to the drive may prove challenging. Don't expect a windows like interface.
      But you could read and write to it just like you would to an SD card.

      Delete
  8. Greatjog Scott. I have a problem with the CH376EVT board. The board with USB and SD.
    Your test program works fine on my Arduino Mega. The problem is that my PC cannot see the TEST4.TXT file create on the Arduino and the Arduino cannot see the files TEST2.TXT and TEST3.TXT created on the PC.
    The USB drive is a Patriot 32GB drive freshly formatted with FAT32 and 16kB clusters.

    Any suggestions?
    Thank you
    Jon W

    ReplyDelete
    Replies
    1. Hi Jon,
      Try using a smaller or different brand of USB stick. I am not sure, but there seems to be an issue with some brands. I used a SanDisk USB stick without any problem. Please let me know how you go.

      Delete
    2. Hi Scott
      Got something figured out: No problem when disk is formatted on Windows XP with default allocation size 4096. Problem when formatted on Windows 10.

      Wondering if Windows 10 was causing problems I fired up an old computer with Windows XP. Formatted the Patriot stick and put a TEST2.TXT and a TEST3.TXT on it. Over to the Arduino, ran your test 4 (OK), 8 and 9 (both OK). Now to the Windows 10 PC. All three files visible and readable.

      To check if the Windows 10 allocated size of 16k the problem was, I I formatted with 4k and 2k. Both experienced the original problem.

      So the formatting on Windows 10 creates the problem.
      Do you have any suggestions for a third party formatting program that I can test on Windows 10?
      Jon W

      Delete
    3. Hi Jon,

      This info is good to know.

      According to ICStation, the module supports the FAT32, FAT16 and FAT12 file formats (up to 32GB). So not sure why your attempts to format using windows 10 is causing issues??

      This is the only program that I could find on the internet so far:
      https://miapple.me/windows-10-format-external-hard-drives-fat32/

      I have not tried it so please make sure you do a virus scan (etc etc).

      Regards
      Scott


      Delete
    4. I forgot to mention that I use Windows 7 - and have no issues, so not sure whether this issue also exists on Windows 8 ??

      Delete
    5. Hi Scott
      The program you suggested works great. Formatted my Patriot stick with both 4k and 16k allocation units and both tests do not show the problem I experienced with the Windows 10 formatter.

      To make sure the problem is not related to the 3Gb Patriot stick, I repeated the test with an 8Gb Kingston stick. Same problem: On a stick formatted with Windows 10, files created on the PC are not visible on the Arduino and the other way around. I even tried a 32kb allocation size without problems.

      I have no way to test on Windows 8.

      My project is a small box to create backups of the pictures on our smart phones and cameras during our long travels.
      SD/Stick to SD/Stick. Our Android phones generate long file names containing date and time. The CH376 is not suitable for long file names and walking a directory tree with unknown file names. The CV376EVT will be the output side. Your test as basis will be converted to support the SD functions and SPI interface.
      Thanks for creating this great tool.
      Jon W

      Delete
    6. Excellent Jon,
      Thanks for your feedback and information. Very helpful for anyone else in the same boat. Am glad that this tutorial helped you/your project. Feel free to post your end project and relevant code/project pictures etc within the ArduinoBasics forum.. and if you are interested, I would be happy to feature it on my blog for others to enjoy.

      Delete
  9. Hi Sir Scott,

    I'm a student...I want to create a 'device' that can read the contents of a flash drive and it has a screen to display the files and also can navigate through the existing files....

    this device will be connected to a printer and will send a command to a printer to print....

    so can I use CH376S for my project? will you give me some help?
    it would really be a great help...

    -Glen

    ReplyDelete
    Replies
    1. I have no idea how you would attach your device to a printer, and I have no exposure with that kind of thing. The only help I can give you is within this tutorial - this is all I know..

      Delete
  10. Hi. I just followed all these instructions, but when I send 1 using serial monitor I get "TimeOut waiting for response: Error while: checking connection". I triple checked wiring. I suspect that my CH376S board is defective. Any suggestions?

    ReplyDelete
    Replies
    1. I don't know why you are getting this error - as that is not programmed into the Arduino Code?? Have you tried using other Serial commands to the Arduino before (without the CH376S module) to see if that part is working ok ??

      Delete
    2. There were some soldering issues. Right now I'm resoldering everything. This is the first, and will be the last time I buy already soldered board :D better to do it myself.

      Thanks for your attention, keep up sharing a knowledge. There is a saying in my country, it says knowledge reduces when you dont share and increases when you do.

      Delete
    3. I think you must have been unlucky. Out of all the pre-soldered boards I have bought, there has only been one or two that have not worked properly. The rest have been fine. But I guess there is a certain level of knowledge gained by soldering it up yourself.. it just depends on how much time you have or are willing to spend doing that. So did you end up getting it to work ?

      Delete
    4. I get the same error. It looks like it is in the code in the waitFor Response function called from the checkConnection function. I am stuck at step 1 . What does it want?

      Delete
    5. Hi Roja, you are right.
      When you send a command message to the module, the module will respond with an appropriate response. If it times-out, it will produce the error as stated above, plus it tells you what part of the process it was trying to do at the time it expected a message back from the module. Ensure the connections to the module are correct, and ensure that the commands sent to the module are correct... It expects a certain number of bytes before a response is generated.

      Delete
  11. hi sir,
    i have a problem in writing process,
    when i write my data in file, i get this message in serial terminal:

    The CH376S module has been reset!
    set_USB_Mode command acknowledged
    USB is present
    Checking USB disk connection status:
    >Connection to USB OK
    Mounting USB disk>USB Mounted - OK
    Setting filename to:test4.txt
    Writing to file:
    may name is alex
    Data Length:16
    Write code (normally FF and 14): 10,14
    Closing file:>File closed successfully.

    why i get 0x10insted of 0xFF,
    please help me,
    sorry for my englsih

    ReplyDelete
    Replies
    1. I am not sure:
      This is the only technical document that I have access to:
      http://www.mpja.com/download/ch376ds1.pdf

      Delete
    2. Make sure the USB is blank.
      I did experience some bugs during testing, but it was a while ago now, and I cannot remember why they occurred or how I fixed it. But use that document that I listed above. It is likely to help you.

      Delete
  12. I found an interesting thing. Command returns not 0xFF, it returns the number of bytes written. And what is important: When you record a lot of data, if the next line exceeds the block size of 512 bytes (512 in my case), it is written only that part that fit into the 512 bytes, the rest is cut off.

    To avoid this, I changed a little bit recording function. I publish an example of its use:

    ..........
    String senda = "2016,3,22,18,33,NAN,NAN,NAN,NAN,38.0,22.0,0\n";
    byte ttl = senda.length();

    for (int i=0; i<120; i++)
    {
    byte sv = fileWriteAppendix(senda); //Write data to the end of the file
    if (svwrite(0x57);
    USB->write(0xAB);
    USB->write(0x3C);
    USB->write((byte) dataLength);
    USB->write((byte) 0x00);
    if(waitForResponse("setting data Length")){ // Wait for an acknowledgement from the CH376S module before trying to send data to it
    if(getResponseFromUSB()==0x1E){ // 0x1E indicates that the USB device is in write mode.
    USB->write(0x57);
    USB->write(0xAB);
    USB->write(0x2D);
    USB->print(data); // write the data to the file
    if(waitForResponse("writing data to file")){ // wait for an acknowledgement from the CH376S module
    }
    dataLength = USB->read();
    USB->write(0x57);
    USB->write(0xAB);
    USB->write(0x3D); // This is used to update the file size. Not sure if this is necessary for successful writing.
    if(waitForResponse("updating file size")){ // wait for an acknowledgement from the CH376S module
    }
    USB->read();
    delay(100);
    }
    }

    return dataLength;
    }

    ReplyDelete
  13. I have gotten my code to work as desired and writing to the USB with the Arduino (Mega) still attached to the computer and sending Serial.print statements to the Serial monitor. I have tried to get it to run stand alone by commenting out all of the Serial.print lines. Then it seems to have problems writing and/or skipping write to usb lines. Is there a proper way to modify code so the Arduino can write to the USB unplugged from the computer?
    Thanks Roja

    ReplyDelete
    Replies
    1. That is a good question. I have never really tested this project with the Arduino's USB cable unplugged from the computer. There are a couple of things that come to mind...
      1) Perhaps try to slow down the write speed somehow (e.g by increasing the delay between each line being written) - or any other way you can think of.
      2) There may be a grounding/power supply requirement for proper operation. Most of the time when I encounter a difference between connected Arduino and standalone Arduino - it is because of a power supply issue. How are you powering the Arduino when it is standalone? Don't use 9V battery. If you have to use a battery, use 4xAA batteries. But even better, use a power source that is connected to the wall (eg. wall-wart/Plugpack or something similar)http://www.altronics.com.au/p/m8925a-powertran-9v-dc-2a-appliance-plugpack/

      Delete
    2. I am using a wall transformer 7VDC. I have tried adding in delay statements but nothing works so far. Sometimes it will create a valid file (although missing data) once after I unplug the cable to my computer, and after that it does not work. I need to get this to write reliably while unplugged for my project to function. Thanks,
      Roger

      Delete
    3. How do I set the device to use hardware serial, since I am using a MEGA board. Maybe that will help?

      Delete
  14. I was able to set up the USB module on the MEGA Serial2. No improvement to my problem. I did realize that a successful USB write seems to depend on the IDE serial window being open at the time of the write, not just that the programming cable is plugged in to the computer and the IDE open. What does that mean? Thanks,
    Roger

    ReplyDelete
    Replies
    1. I have no idea - perhaps take this one to the Arduino.cc forum. Someone with a bit more knowledge than me may be able to help you. But I don't understand why that is happening?

      Delete
    2. I found that if i put the USB in and reset the Arduino, then files will write properly (un tethered from the computer)as long as it is not taken out. Each re insertion of the USB stick requires a reset. Don't know why, but it seems to work.
      Roger

      Delete
    3. Each time the USB is connected, you probably need to put it through the initialisation sequence - or reset the module?? It has been a while since I used it, but from memory, I thought the module sent a message when a USB was connected and disconnected. Perhaps you could use this to automatically reset/reinitialize?

      Delete
  15. would it be possible to read certain data in usb flash disk and use it to trigger the output? and when that data not detected(usb unplug) it will trigger the output to be off with delay.

    ReplyDelete
  16. hi, i can txt file but this txt file doesn't view on computer. Used space of usb drive increases but i can't see this txt file. why?

    ReplyDelete
    Replies
    1. Make sure your file name is all caps.

      Delete
    2. i did all caps but i still same,as well arduino doesn't read txt file created on pc. My purpose, some datas saved usb drive with arduino and i take this datas on pc. is there another method or devices like ch376?

      Delete
  17. Hi scott, do you have the fritzing sketch?

    ReplyDelete
    Replies
    1. Hi Miguel - I don't save my Fritzing sketches - they take about 5-10 minutes to make. I make them from scratch each time.

      Delete
  18. Hello and thank you for your wonderful job!!
    I tried the above sketch, I have problem with writing, I got:

    COMMAND4: Create and Write to File : TEST4.TXT
    ----------------------
    The CH376S module has been reset !
    set_USB_Mode command acknowledged
    USB is present
    Checking USB disk connection status
    >Connection to USB OK
    Mounting USB disk
    >USB Mounted - OK
    Setting filename to:
    File could not be created, or it already exists
    Closing file:
    >File closed successful

    there is no file'name after "Setting filename to:", after investigating for a while, I noticed that if wrData is short (just 5 characters) it works, otherwise no.
    what's wrong? (using W7-x64, arduino 1.6.9)
    thanks

    ReplyDelete
    Replies
    1. Not sure - that might be a limitation of the module... I have no idea. Perhaps there is something within this document that may help you?

      http://www.mpja.com/download/ch376ds1.pdf

      See the section named: "5.15. CMD_SET_FILE_NAME"

      Delete
  19. Replies
    1. No - unfortunately not.
      Have never come across the CH375B before, but I am guessing that you would operate it in a similar way??
      This may help: ftp://imall.iteadstudio.com/Modules/IM131009001/DS_IM131009001.pdf

      Delete
  20. File Size Definition procedure is incorrect.
    Please, replace everywhere

    int fs = getFileSize(); //Get the size of the file

    to

    unsigned long int fs = getFileSize(); //Get the size of the file

    and

    unsigned long int getFileSize(){
    unsigned long int fileSize=0;
    Serial.println("Getting File Size");
    USB.write(0x57);
    USB.write(0xAB);
    USB.write(0x0C);
    USB.write(0x68);
    delay(100);
    Serial.print("FileSize =");
    if(USB.available()){
    fileSize = fileSize + USB.read();
    }
    if(USB.available()){
    fileSize = fileSize + (USB.read()*256);
    }
    if(USB.available()){
    fileSize = fileSize + (USB.read()*256*256);
    }
    if(USB.available()){
    fileSize = fileSize + (USB.read()*256*256*256);
    }
    Serial.println(fileSize);
    delay(10);
    return(fileSize);
    }

    ReplyDelete
    Replies
    1. Thanks anonymous,
      You could have just said to change all ints to unsigned longs

      But still, thanks for the feedback...

      Delete
  21. Not only change all ints to unsigned longs, but change
    ...
    (USB.read()*256*256*256);
    ...
    instead
    ...
    (USB.read()*255*255*255);
    ...
    Thanks

    ReplyDelete
    Replies
    1. Excellent - Thanks Anonymous - I missed that in your original post.
      Thanks for picking this up.

      Delete
  22. Hello, I use ch376s module to save data into USB memory stick but using this, I have some difficulties

    1st, now I save data in main root of USB memory but I'm trying to make a folder in main root and make files in that folder. So, I wonder how I can make folder and create file to save data.

    2nd, now I make a file using writeFile function and through appendFile function, write data sequentially. Size of one data is 21 byte and I save data in alternate lines.
    But when arduino send 100 of data, only 80 of data are saved in USB memory. So, I wonder what reason of this data loss

    Thank you .

    ReplyDelete
    Replies
    1. 1. Don't know how to make folders on this module... so cannot help you.

      2. Don't know why it is not saving all of your data.

      Delete
  23. Sir I have problem about why after power connecting ch375 module & i am insert usb pen drive but not turn on ch375 module led how to power on module???

    ReplyDelete
    Replies
    1. Hi Ravindra,
      You have to send a few commands to the CH375 module before it starts monitoring the USB port. Try commands 1 and then 2 as described in the tutorial above - please watch video for demonstration.

      Delete
  24. Hello Scott,

    I've found about your page while trying to solve issues with my CH376 module.

    I can create, write, edit directory info (file date) without error but when looking at the result on a computer, created files are visible but of size 0 byte and cannot be opened by windows ("file not found" error when clicking on the file). More over, the file date is 2004 which means the directory info edit wasn't too effective.

    I've dumped the flash drive data (raw data of the flash memory) and I can see the file data is actually saved to the flash drive but failed to be connected to the directory data.

    I've tried with three flash drives of different capacity, state and manufacturing date, same issue.

    Is that something you experienced before? I would expect such IC to have much greater compatibility than this. So far it's 0% compatibility for me. I'm sort of thinking that module is glitchy/junk and I wonder if any alternatives available.

    Thank you.
    Daniel

    ReplyDelete
    Replies
    1. Hi Daniel,

      I know that you have to do each task in a particular order, otherwise it has issues. Also I think it has issues with some file names. I cannot really remember because it has been a long while since I have done this tutorial. Also - have you tried creating a blank text file in windows and then try to append to that file... does that work ?
      There may also be some stipulation with regard to the file format used on the USB stick (FAT16 and FAT32??). You may need to format the USB drive in this file format ??

      What message are you getting from the Serial monitor when writing to the USB stick?

      And lastly, I think there are some slightly different models floating around on the internet, which look "almost" identical, but are not. I got mine from ICStation - you can find the link in the "Parts Required" section of this tutorial.

      Delete

Feel free to leave a comment about this tutorial below.
Any questions about your particular project should be asked in the ArduinoBasics forum.

Comments are moderated due to large amount of spam.