Pages

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.

/* =============================================================== 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 computerbyte USB_Byte; //used to store data coming from the USB stickint 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//==============================================================================================================================================voidsetup() {
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 LEDdigitalWrite(LED,LOW); // Turn off the LED
}
//================================================================================================================================================voidloop() {
if(Serial.available()){
computerByte = Serial.read(); //read any incoming bytes from the Serial monitor, and store this byte in the variable called computerByteif(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 moduleSerial.print("CH376S has just sent this code:");
Serial.println(USB.read(), HEX);
}
}
//END OF LOOP FUNCTION ========================================================================================================================================//print Command headervoid 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 sequencevoid 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 connectedSerial.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 = FFHSerial.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 readingint 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 nameif(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 withvoid 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 statusvoid 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 successfulSerial.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 diskvoid 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 successfulSerial.println(">USB Mounted - OK");
} else {
Serial.print(">Failed to Mount USB disk.");
}
}
}
//fileOpen========================================================================================//opens the file for reading or writingvoid 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 filevoid 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 byteswhile(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 bytewhile(USB.available()){
Serial.write(USB.read()); //Send the data from the USB disk to the Serial monitordelay(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 filevoid 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 itif(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 fileif(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 0xFFSerial.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 fileboolean 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 filevoid 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 successfulSerial.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 successfulSerial.println("Pointer successfully applied");
}
}
}
//fileClose=======================================================================================//closes the filevoid 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 Sizeif(waitForResponse("closing file")){ // wait for a response from the CH376S. byte resp = getResponseFromUSB();
if(resp==0x14){ // CH376S will send 0x14 if this command was successfulSerial.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 commanddelay(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 secondvoid 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 :

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.

Nice Job. Thank youI 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

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.

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.

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.

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

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.

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.

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.

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

Hi ScottThe 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

Hello Scott and hello Jon,First thanks for this tutorial. I am trying to use the module ch376 controlled by spi using a Pic24 so i rewrote the program. The communication with the module works, i am able to mount the disk and receive response but i am not able to open the file for read. I think i am having a problem related to the usb formatting. I used a unbranded usg 4gb formatted fat32 using windows 7. What specific tool could i use for format the flash drive?

"What specific tool could i use for format the flash drive?"Perhaps the one mentioned in the comment above ?

Having said that, I did not need to use that program. I just used a new SanDisk USB stick as seen in the pictures/video.Are you getting any error messages when trying to open the file for reading?Please read all comments, as some people have identified better methods for reading from the text file.

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?

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 ??

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 ?

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.

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.

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:

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); } }

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

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/

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

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

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

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?

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?

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

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

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.

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.

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.

If you are using the Arduino Mega, then there is probably no need to use Software Serial. The Mega has three additional serial ports. It would probably be better if you used them.See:https://www.arduino.cc/en/reference/serial

So you would need to change this line (and all the lines related):SoftwareSerial USB(10, 11);

What i found, the problem is in read data function(0x27) and in reading sequence.In the read data function the first incoming byte is the lenght of data block.That means if your request is 64byte but you get 8byte data block lenght then you request just 8 bytes instead of 64. The sequence is incorrect. 1,Set bytes to be read(0x3A) -- 2,Read data(0x27) -- 3,Update pointer for reading(0x3B).The CH376 chip read the data to buffer(512 byte), that 512 byte we read out with 1(0x3A), 2,(0x27) and when the last byte is read out then we call 3,(0x3B) that command load the next 512 byte to buffer and we start again the 1,2 sequence. I hope is it understandable :) . If not then i can draw some flowchart

Thanks Gyorgy,Ok - I think I understand what you mean now.This should help others looking to read large amounts of data.One day I may go back and revisit and test it out.Thank you for solving this piece of the puzzle.

Hi, Thanks much for this tutorial. Best resource I got so far regarding the possibility of file transfer from my data logger into a stick drive. Multiple resources regarding data logging in SD but all sketches I looked into so far are either real-time monitoring hooked in a computer, or practically prying out the SD Card from the data logger shield and then reading it via usb/SD adapter. My project would require placing the arduino and data logger et al hardware in a semi-permanent enclosure. It would store time/temperature data of a usual 3-4 hour cycle. From your point of view Scott, would it be possible to insert a SD stick in a USB port and the sketch inside the arduino automatically copy/transfer files from the SD card in the data logger. This would eliminate the need to open up the enclosure and pull out the arduino and pry out the SD card from the data logger, every 4-hour cycle or so. Would greatly appreciate your insight on this, and maybe guidance to get me to the right direction.

I think it would be good to include a button in the Arduino sketch to signal your intention to copy the data. But I think it should be possible to transfer the data from the SD card to the USB thumb drive. However, I have never done that myself - so I am just speculating.

Hi, really a great job. I want to implement this. I want to send data available on serial monitor of master arduino to slave arduino using this. I dont want to write to any file just want to send data from master to slave. Is it possible to do so..?

Hi Scott, thank you for sharing!I'm trying send use the same chip (ch376s) to tansfer data from atmegaxxx(I didnt choose which module yet) to usb memory stack and PC. so can I do this, and I need in the code in order to be able to trasfer data to PC via ch376s?

Hi Samah,I am not clear on what you are trying to do.If you want to transfer data from the Arduino to PC, then just transmit it to the PC using Serial commands and have the Arduino connected via USB. Depending on your setup, you could also do this via ethernet cable, bluetooth or wifi (etc).In the tutorial above, I show you how to read from the USB stick and transmit it to the computer, which is what I think you are asking how to do ??

I want my atmega to standalone so I can't use the serial port of Arduino, on other hand, I want to use ch376s chip for two purposes; the first, to use it in USB-Host mode to transfer data to usb disk; the second, to use it in USB-Device mode to communicate with PC and transfer data. My question is: is this possible? and if yes, how can I do it? Because in your code, you mentioned that you sent 0x06 to the chip to activate usb-mode,but according to the datasheet, this activate the USB-Host mode, so logically and according to the same datasheet, I only need to send 0x01 or 0x02 (I'm not sure which one is the right one) to the chip in order to turn the chip to USB-Device mode and start the communication between PC as host, and atmega as a device. So again! is this possible?

The datasheet is where I find most of my answers, so you are not alone there. You could also try asking the manufacturer as previously suggested, or alternatively trying your luck on the Arduino forum.https://forum.arduino.cc/

I am currently working on a few projects involving a SD card, and one with the CH375. I’ve had trouble getting my code to work so far. In the case of the CH375 I am talking to it via a parallel bus and believe the basic communication is working. I can send one of the commands which then triggers an interrupt, and then read the status and see the interrupt signal go in-active. Every time the status is 22 hex.

I’m wondering if part of my problem could be the FAT issue - all of the USB drives I’ve tried were formatted on my Windows 10 PC. I saw mention of the program you used to format a drive to one of the earlier FAT standards. Can you tell me what the exact program name is ? At the link you gave, there is mention of two programs ... so, I just want to make sure I am getting the one you said worked for you.

I did a little search and found this link which explains how to do the format at the DOS command prompt using the MS DISKPART command. I tried it with my SD card project - but still haven’t achieved success. Haven’t had time to use if on my USB drives and my CH375 project.

Hi Dave,I don't remember having to format the USB Flash drive, but know from the datasheet that it only supports FAT16, FAT32 and FAT12 filesystems. I think there is a link in the comments above that mentions a particular program that you can use, but I have never tried it and cannot verify if it even works... so take normal internet precautions with that one (i.e. Virus scan etc).

My only advice is to try one step at a time, get the basics working, ensure you are getting a response from the module etc. And then increase the functionality from there.

Maybe one day, but unlikely to be in the near future.I have moved on to other projects. But if I had to do it, I would, 1) First learn how to communicate to modules using serial cable.2) Transmit the serial code as per the datasheet or tutorial.3) I would probably need to work out what software is required to transmit the necessary code.

It should not be too hard to adapt, but I don't have the time for that particular project right now. Sorry.

Hello,Actually im working on Arduino Uno microcontroller and i'm trying to control a vidéo USB Module.

To do that i am using an USB module CH376s which will be the bridge between the Arduino and the Video Module.

Actually the video module is considered like an USB key when i plugeed it to the computer i cant put any folder inside and when i put a video folder and eject (unmount) the module the video is played.

so i'm trying to do that using the arduino but i get a problem, when i try to create a file on the video module i lose the mount.

and what is bizzar to me is that when i stop in the mount step and i don't do anything else the module still mounted but when i send any command via the USB module (CH376) i lose the mount.

when i do the same steps for a normal USB key i don't get any problem.

That's it i'm bloqued and i can't figure out the problem i hope that you get a little idea about my problem and i hope you could help me.PS : regarding the ch376 datasheet it suport UFI command but i don't know how i could send UFI command through the CH376;

MOMO - sorry - I have no idea.My knowledge is contained in the tutorial.I haven't done anything more than what you see right here.The datasheet is the only guide I can point you to.Unfortunately, the datasheet is not that clear.