.
CH376S USB Read/Write module Reviewed by Unknown on 21:07 Rating: 4.5

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.




Description: CH376S USB Read/Write module Rating: 3.5 Reviewer: Unknown ItemReviewed: CH376S USB Read/Write module
Simple Arduino Serial Communication Reviewed by Unknown on 23:40 Rating: 4.5

Simple Arduino Serial Communication

This Tutorial is progressive and will be updated from time to time. The goal is to start from a very basic form of Arduino Serial communication, and progressively add or improve components so that we can ultimately transmit data from one computer to another using an XBee.
Please note: I am not an expert, but am happy to share what I have learned. The Arduino forums are a great place to ask questions, feel free to link to this blog (if required).

Let us begin.


Stage 1: ECHO ECHO                                                                                    


Parts Required:

  • Computer
  • USB cable
  • Arduino UNO (or equivalent)
  • Arduino IDE

The following code will make the Arduino ECHO anything you send to it. Therefore, if you type a 3, the Arduino will send back a 3. If you type a letter F, the Arduino will send back a letter F. 
Enter the following code into your Arduino IDE and upload it to your Arduino.


Arduino Sketch


 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
/* Simple Serial ECHO script : Written by ScottC 03/07/2012 */

/* Use a variable called byteRead to temporarily store
the data coming from the computer */
byte byteRead;

void setup() {
// Turn the Serial Protocol ON
Serial.begin(9600);
}

void loop() {
/* check if data has been sent from the computer: */
if (Serial.available()) {
/* read the most recent byte */
byteRead = Serial.read();
/*ECHO the value that was read, back to the serial port. */
Serial.write(byteRead);
}
}
The above code was formatted using this site


Instructions

1. Once the Arduino sketch has been uploaded to the Arduino. Open the Serial monitor, which looks like a magnifying glass at the top right section of the Arduino IDE. Please note, that you need to keep the USB connected to the Arduino during this process, as the USB cable is your communication link between your computer and the Arduino.

Serial Monitor Icon


2. Type anything into the top box of the Serial Monitor and press <Enter> on your keyboard. This will send a series of bytes to the Arduino. The Arduino will respond by sending back your typed message in the larger textbox.

Serial Monitor Communication


3. Please note that we are using Serial.write(byteRead); on line 18 to get the Arduino to ECHO the message back to you on your computer. 



Things to Try 

1. Delete lines 16 to 18, and replace them with the following line :

               Serial.write(Serial.read());

This essentially eliminates the byteRead variable in the sketch above. But we will be using it later on, so once you have tested it out, put the code back together as originally displayed.


--------------------------------------------------------------------
2. Replace line 18 with a Serial.println instead of Serial.write

               Serial.println(byteRead);

Once uploaded, type 1 <enter> 2 <enter> 3 <enter>  into the Serial Monitor.
You should see:

49
50
51

Serial.print and Serial.println will send back the actual ASCII code, whereas Serial.write will send back the actual text. See ASCII codes for more information.


--------------------------------------------------------------------
3. Try typing in numbers like 1.5  or  2.003  or  -15.6 into the Serial Monitor using Serial.write and Serial.print or Serial.println commands as described before.

You will notice that the decimal point transmits as a number using Serial.print  or Serial.println, and will transmit as a decimal point when using Serial.write






STAGE 2: Delimiters                                                                                


How do you handle 2 or more numbers when sending or receiving?
Let us say that you have number pairs that you want the Arduino to interpret. How do you separate the numbers? The answer is Delimiters.
You may be familiar with CSV (comma separated value) files, where each field is separated by a comma (,). The comma is a useful way of separating or grouping information.

Lets say you have the following stream of numbers:
12345678910

How will your Arduino know if this is a single number, or a series of numbers?
Eg:

12,34,56,78,91,0
123,456,78,910
1,2,3,4,5,6,7,8,9,10
12345678910

The comma delimiters help to identify how the numbers should be interpreted.

 In the echo example in Stage 1 above, you would have noticed that when we used Serial.print(byteRead); that the values displayed one after another in a similar fashion to 12345678910.

You would have also noticed that Serial.println(byteRead); provided a line break between each value sent. And depending on the numbers sent, it could have looked like this:
12
34
56
78
91
0

The Serial.println() function essentially uses a line feed to separate the values being sent. This line break can be used as a delimiter, but we will look at that later on. For now we will concentrate on using a comma (,).

We will now get the Arduino to "listen" for the comma to help it identify a new number.
According to the ASCII code site, a comma is equal to byte code 44. So when the Arduino reads a byte code that is equal to 44, we will get it to print a line feed.


Enter the following sketch into your Arduino IDE and upload it to your Arduino.

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
/* Simple Serial ECHO script : Written by ScottC 04/07/2012 */
/* Stage 2 : Delimiters */

/* Use a variable called byteRead to temporarily store
the data coming from the computer */
byte byteRead;

void setup() {
// Turn the Serial Protocol ON
Serial.begin(9600);
}

void loop() {
/* check if data has been sent from the computer: */
if (Serial.available()) {
/* read the most recent byte */
byteRead = Serial.read();

/*Listen for a comma which equals byte code # 44 */
if(byteRead==44){
Serial.println();
}else{
/*ECHO the value that was read, back to the serial port. */
Serial.write(byteRead);
}
}
}
The above code was formatted using this site


Instructions

1. Once the code has been uploaded to the Arduino, open the Serial Monitor once again and type the following sequence of numbers:

1 <enter>  2 <enter> 3 <enter>

You should see the Serial monitor display the following number:    123




--------------------------------------------------------------------
2. While still in the serial monitor, type the following:

, <enter> 1 <enter> , <enter> 2 <enter> , <enter> 3 <enter> ,

Please note the commas between each numerical entry. You should now see a pattern like this:
1
2
3




--------------------------------------------------------------------
3. While still in the serial monitor, type the following:

12,23,34,45, <enter>

Please note the commas between each numerical entry. You should now see a pattern like this:
12
23
34
45

You will notice that the commas have been replaced by line feeds, and each number should display on a new line.



--------------------------------------------------------------------
4. While still in the serial monitor, type the following:

1,,2,,3, <enter>

You should see the following pattern:
1

2

3


So hopefully that explains the concept of delimiters and how they can be used to separate a stream of numbers, no matter how long it takes to get to the Arduino. We used an IF-statement to listen for the comma, but we could have used any other delimiter provided we knew the byte code.

We did not identify how to send delimiters FROM the Arduino, but we will get to that I promise. It is not that hard, and uses a similar principle. I am sure you can work it out, if not, stay tuned.




STAGE 3: Arduino Maths: Simple addition                                                  


In this stage, we are going to get the Arduino to do simple maths. We will send it two integers (or whole numbers), and the Arduino will do the hard work and send us the answer in no time at all.
This might seem like a simple task, but when you send a number like 27 to the Arduino, it does not receive the number 27. It receives 2 and then 7 in byte form. In other words, the Arduino will see the byte codes 50 and then 55 as per the ASCII table on this page.

One way to convert this byte code back to a 2 and a 7 is to subtract 48 from each byte received, providing the byte is in the range 48 to 57 inclusive (which equates to the numbers 0-9).
We are not done yet. We then need to join these numbers to make 27.

Step1: Subtract 48 from the bytes received, only if the bytes are in the range 48 to 57.
                 Example:    50 - 48 = 2
                                    55- 48 = 7

Step2: Multiply the previous number by 10, before adding the most recent byte received.
                 Example:   (2 x 10) + 7 = 27

If we have a number like 1928, then we would create this number using the following calculation
                                   1 =                         1
                   (1 x 10) + 9 =    10 + 9   =   19
                (19 x 10) + 2  = 190 + 2   =  192
              (192 x 10) + 8  = 1920 + 8 = 1928

Step3: Use a "+" sign as a delimiter so that the Arduino can move onto the Second number

Step4:  Capture the second number as per Step2. An "=" sign will tell the Arduino that it has reached the end of the second number, and to proceed to step 5.

Step5:  Add the 2 numbers together and send back the answer.



The following code will carry out the 5 steps above.
Enter the following sketch into your Arduino IDE and upload it to your Arduino.

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
/* Simple Serial ECHO script : Written by ScottC 05/07/2012 */
/* Stage 3: Arduino Maths: Simple Addition */

/* Global variables needed for programming workflow
byteRead: holds the value being read from the COM port
num1: holds the entire first number
num2: holds the entire second number
answer: holds the sum of num1 and num2
mySwitch: enables the switch between num1 and num2 */

byte byteRead;
long num1, num2,answer;
boolean mySwitch=false;

void setup() {
/* Turn the Serial Protocol ON and
initialise num1 and num2 variables.*/
Serial.begin(9600);
num1=0;
num2=0;
}

void loop() {
/* check if data has been sent from the computer: */
while (Serial.available()) {
/* read the most recent byte */
byteRead = Serial.read();

//listen for numbers between 0-9
if(byteRead>47 && byteRead<58){
//number found

/* If mySwitch is true, then populate the num1 variable
otherwise populate the num2 variable*/
if(!mySwitch){
num1=(num1*10)+(byteRead-48);
}else{
num2=(num2*10)+(byteRead-48);
}
}

/*Listen for an equal sign (byte code 61)
to calculate the answer and send it back to the
serial monitor screen*/
if(byteRead==61){
answer=num1+num2;
Serial.print(num1);
Serial.print("+");
Serial.print(num2);
Serial.print("=");
Serial.println(answer);

/* Reset the variables for the next round */
num1=0;
num2=0;
mySwitch=false;

/* Listen for the addition sign (byte code 43). This is
used as a delimiter to help define num1 from num2 */
}else if (byteRead==43){
mySwitch=true;
}
}
}

The above code was formatted using this site


Instructions


1. Once the code has been uploaded to the Arduino, open the Serial Monitor once again and type the following sequence:

         1+2=   <enter>

You should get the following message sent back to Serial Monitor

         1+2=3



Things to Try

1.   Enter this sequence:
              10   <enter>
               +   <enter>
              10  <enter>
               =   <enter>

       Result:     10+10=20

--------------------------------------------------------------------
2.   Enter this sequence:
             10  <enter>
             20  <enter>
             +5= <enter>


      Result:   1020+5=1025




--------------------------------------------------------------------
3.   Enter this sequence:
             10+20+30=   <enter>


      Result:    10+2030=2040

I have specifically written this script to add two whole numbers together. If you start to introduce more complicated calculations, the results become unpredictable.

--------------------------------------------------------------------
4.    Enter this sequence:
           1.2+1.0=    <enter>

      Result: 12+10=22

Once again, I have only designed this script to handle whole numbers. Therefore, decimal points are ignored.

--------------------------------------------------------------------
5.  Enter this sequence:
          -5 + 10=     <enter>


     Result:    5+10=15


This script ignores the negative sign, and treats the -5 as a positive 5.


I have done this on purpose. I wanted to show you how the Arduino reads numbers from the com port, and how easy it is to exclude vital functionality in your code. I have kept this script simple, however, if you wanted to, you could make the Arduino deal with each of the above situations and more.  Multiplication, division and subtraction is handled in the same way.

This is the last thing I want you to try before we go to the next stage:

6. Enter this sequence:
           2147483646+1=  <enter>           Result:  2147483646+1=2147483647
           2147483647+1=  <enter>           Result: 2147483647+1=-2147483648


Note that the maximum size of a "long" number is 2147483647. If you add one to this number, the result is equal to the minimum size of a "long" which is -2147483648.




STAGE 4:  Sending doubles to Arduino : The double doubler             

Now we get to some tricky business. Sending and receiving Doubles (to and from) the Arduino.

Up until now, I have tried to keep it simple using whole numbers, but there will come a time when you will want to send a fraction of a number through the Serial line.
To test our program, we will want to send a very small number to the Arduino, multiply the number by 2, and return it back.

Our final test is to try a number like :  0.000001
             and then a number like:   123.321


IMPORTANT NOTE:   When the Arduino sends a float or a double through the COM port using Serial.print() or Serial.println(), it will automatically send the number to 2 decimal places.
A number like 1.2345 will appear as 1.23,   and a number like 1.9996 will appear as 2.00
To demonstrate this, we will get the Arduino to send these floats/doubles to the Serial Monitor.


Enter the following sketch into your Arduino IDE and upload it to your Arduino.

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
/* Stage 4: Simple Transmission of a Double
Written by ScottC on 7/7/2012 */

/* Declare the doubles that will be sent to the Serial Monitor */
double myDub1, myDub2;

/* This part of the program only runs ONCE */

void setup(){

/* Turn ON Serial Communication */
Serial.begin(9600);

/* Assign a value 1.2345 and 1.9996 to the Doubles being sent */
myDub1=1.2345;
myDub2=1.9996;

/*Send the values to the Serial Monitor */
Serial.print("myDub1 (1.2345) : ");
Serial.println(myDub1);
Serial.print("myDub2 (1.9996) : ");
Serial.println(myDub2);
}


void loop(){
//Loop does nothing
}
The above code was formatted using this site

When you open the Serial monitor (after you have uploaded the sketch above), you will notice the following output:


          myDub1 (1.2345) : 1.23
          myDub2 (1.9996) : 2.00



The blue text represents the string (or array of characters) being sent using lines 19 and 21.
The red text represents the actual double being sent using lines 20 and 22.

You will notice that myDub2 rounds to 2.00.  This may or may not be what you want.
If you wish to increase the number of decimal places, then you will need to change lines 20 and 22 to the following:

20         Serial.println(myDub1,4);
22         Serial.println(myDub2,4);

The number 4 highlighted in red, indicates the number of decimal places you wish to send.
Try it ! And try changing this number to something bigger or smaller.

---------------------------------------------------------------------------------------------------
Ok - now that we understand this little Serial.print(double,decimals) trick, we will now get the Arduino to echo back a Double.

Before we jump in, perhaps we should try and map out our strategy. For this we will choose a simple decimal to make it easier. So in this example, we will choose 0.1
Once we get this working, we can then do our final test (as mentioned above).

If we send 0.1 to the Arduino, it will read the following byte code

48                    0
46                    .
49                    1

We can use the decimal point as a delimiter.
We will use the following 5 steps to echo the double back to the Serial Monitor:

Step1: Arduino collects all numbers before the decimal point using the same technique as in Stage3.

Step2: When the Arduino receives byte code 46, it will go into decimal mode.

Step3: The Arduino will collect numbers after the decimal point using a similar technique to step1.

Step4: Use maths to create the double, and then multiply it by 2

Step5: Display the doubled Double value in the Serial monitor.



Enter the following sketch into your Arduino IDE and upload it to your Arduino.

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
/* Simple Serial ECHO script : Written by ScottC 06/07/2012 */
/* Stage 4: Double doubler */

/* Global variables needed for programming workflow
------------------------------------------------------
byteRead: holds the value being read from the COM port
num1: holds the number before the decimal point
num2: holds the number after the decimal point
complNum: holds the complete number (before multiplation)
answer: holds the final value after multiplication
counter: is used to convert num2 to the number after the decimal
numOfDec: counts the numbers after the decimal point
mySwitch: enables the switch between num1 and num2 */

byte byteRead;
double num1, num2;
double complNum,answer,counter;
int numOfDec;
boolean mySwitch=false;


void setup() {
/* Turn the Serial Protocol ON and
initialise num1 and num2 variables.*/
Serial.begin(9600);
num1=0;
num2=0;
complNum=0;
counter=1;
numOfDec=0;
}

void loop() {
/* check if data has been sent from the computer: */
while (Serial.available()) {
/* read the most recent byte */
byteRead = Serial.read();

//listen for numbers between 0-9
if(byteRead>47 && byteRead<58){
//number found

/* If mySwitch is true, then populate the num1 variable
otherwise populate the num2 variable*/
if(!mySwitch){
num1=(num1*10)+(byteRead-48);
}else{
num2=(num2*10)+(byteRead-48);

/* These counters are important */
counter=counter*10;
numOfDec++;
}
}

/*Listen for an equal sign (byte code 61)
to calculate the answer and send it back to the
serial monitor screen*/
if(byteRead==61){
/* Create the double from num1 and num2 */
complNum=num1+(num2/(counter));

/* Multiply the double by 2 */
answer=complNum*2;

/* Send the result to the Serial Monitor */
Serial.print(complNum,numOfDec);
Serial.print(" x 2 = ");
Serial.println(answer,numOfDec);

/* Reset the variables for the next round */
num1=0;
num2=0;
complNum=0;
counter=1;
mySwitch=false;
numOfDec=0;

/* Listen for the decimal point (byte code 46). This is
used as a delimiter to help define num1 from num2 */
}else if (byteRead==46){
mySwitch=true;
}
}
}
The above code was formatted using this site



Things to Try


1. Type the following into the serial monitor:

       1.2=  <enter>                             Result:   1.2 x 2 = 2.4

Make sure that you type the equal sign (=) before you press enter, otherwise the Arduino will not know that you are finished, and will not send anything back.

--------------------------------------------------------------------
2. Type the following into the serial monitor:

      100.001=  <enter>                      Result:   100.001 x 2 = 200.002

You will notice that the Arduino is formatting the decimal to the SAME number of decimals as that entered.
This is controlled by the variable: numOfDec.
---------------------------------------------------------------------
3. Now for our final test: Type the following into the serial monitor:

    0.000001= <enter>                       Result: 0.000001 x 2 = 0.000002

First test: PASSED

----------------------------------------------------------------------
4. Type the following into the Serial monitor for our last test:

     123.321=  <enter>                      Result: 123.321 x 2 = 246.642

Second test: PASSED
-----------------------------------------------------------------------

BEWARE: While everything looks perfect, let me tell you that it isn't. But hopefully this code will help you get on the right track. If you decide to type in a number like 123123.111222, you will not get the answer you expected. 
I have found that this program will work if the amount of numbers before and after the decimal point are less than about 9.  Eg. 1234.1234   will produce the right result.
However, 11111.2222 will NOT, because there are 9 numbers represented.

I think this has something to do with the memory allocated to a double, but I am not sure. 
I don't know if people work with these types of numbers, but I am sure there is a workaround, and I am sure someone out there can work it out. I don't personally need this kind of precision, but thought to mention it just in case you do.


-----------------------------------------------------------------------
-----------------------------------------------------------------------

STAGE 5:  Sending sensor data to the Serial Monitor             



We know the Arduino is very good at copy-Cat games, how about getting the Arduino to send us some data from one of our sensors. We will use the Serial Monitor to view the sensor data.

Disconnect the USB cable, and hook up one of your favourite analog sensors to your Arduino. For simplicity, I am going to hook up a potentiometer as per the Fritzing sketch below.

Parts Required


  • Arduino UNO (or equivalent)
  • Computer with USB cable
  • Breadboard
  • Potentiometer
  • 3 Wires


Arduino Fritzing Sketch


Arduino connected to a Potentiometer


     



















Once you have attached your sensor to the board, plug your USB cable into the Arduino, and upload the following sketch.


Arduino Sketch


 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
  /* Stage 5: Send Sensor Value to Serial Monitor
Written by ScottC on 7/7/2012 */

int sensorVal = 0;

void setup() {
// Setup Serial communication with computer
Serial.begin(9600);
}

void loop() {
// Read the value from the sensor:
sensorVal = analogRead(A0);

// Send the value to the Serial Monitor
Serial.print("Sensor Value=");
Serial.println(sensorVal);

// Interval between readings = 1 second
delay(1000);
}
The above code was formatted using this site




Instructions

1. Open the Serial monitor and watch the readings change depending on the input conditions. In my case, by turning the potentiometer from left to right, I get an output similar to the picture below.

Serial Monitor with Sensor Readings



















As per the Arduino reference site, AnalogRead returns an integer between 0 and 1023. You can see this is true based on the picture above. But what if we do not want a value between 0 and 1023. Let us say we want a value between 0 and 100?

You would have to use the map function. We will do it by changing line 13 to this:

13  sensorVal = map(analogRead(A0),0,1023,0,100);

The map function is quite a cool function, and good fun to play around with. So here are some things to try.

Things to Try

1. Change line 13 to the following, upload to the Arduino 
    and then open the Serial Monitor to see the effect.

Trial 1:
13  sensorVal = map(analogRead(A0),0,1023,100,0);

Trial 2:
13  sensorVal = map(analogRead(A0),0,1023,0,1000);

Trial 3:
13  sensorVal = map(analogRead(A0),200,800,0,100);


In Trial 1: We see that the values have been inverted. Instead of ranging from 0 up to100, they now go from 100 down to 0.

In Trial 2: The analog readings are now mapped to a range of 0 up to 1000. 

In Trial 3: The analog readings that range from 200 to 800 are mapped to a range of 0 to 100. Therefore if the analog readings drop below 200, we will end up with a negative value for sensorVal. 
If the analog readings go above 800, we will end up with a value greater than 100.  For this particular example, my readings actually range from  -33 to 137.

Therefore an Analog reading of 0 = -33
                 Analog reading of 200 = 0
                 Analog reading of 800 = 100
               Analog reading of 1023 = 137


----------------------------------------------------------------------------------
What if we don't want the output to go beyond our intended limits of 0 to 100?
Then you would have to use the constrain function. This essentially trims the reading range of the sensor, and sets a minimum and maximum value.

Replace line 13 with the following code:

13  sensorVal = constrain(map(analogRead(A0),200,800,0,100),0,100);

Therefore an Analog reading of 0 = 0
                 Analog reading of 100 = 0
                 Analog reading of 200 = 0
                 Analog reading of 800 = 100
                  Analog reading of 955 = 100
               Analog reading of 1023 = 100
Analog values between 200 and 800 will produce a result between 0 and 100.

-------------------------------------------------------------------------------------

If you wish to continue with this tutorial (stage 6 and above), please follow this link:  Serial Communication Stage 6 and above 




 
 



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.



Description: Simple Arduino Serial Communication Rating: 3.5 Reviewer: Unknown ItemReviewed: Simple Arduino Serial Communication
Back to top