Fads to Obsessions and Beyond...




Free domain for life, exceptional technical support, website transfer

ATtiny85 Serial Comms (RS232)

Demonstrate serial communications with ATtiny85/Digispark using software UART (ATtiny85 has no hardware UART) with external hardware (USB to TTL converter cables and USB To RS232 TTL PL2303HX module/adapter). This enables communications with serial devices (e.g. HC-05 Bluetooth) and or PC (with RS232 port and/or USB TTL converter) for debugging etc.

While many applications require a "myriad" of GPIO's for controlling various outside peripherals and or input from sensors (and hence need "larger" microcontrollers with multiple ports), in the DIY arena there are also many occasions where there are only a couple of inputs/outputs required to be controlled.

In this scenario the ATtiny range of microcontrollers are advantageous in terms of physical size and cost of the GPIO/onboard peripherals available. Particularly when considering that an ATtiny85 on a Digispark "development" board can be purchased from ebay for ~$1 (which provides a surface mounted ATtiny85 with 5V regulator and PCB tracks to enable insertion into a USB socket). The ATtiny85/Digispark coupled with the Arduino IDE for programming enables "simple" projects to be developed quickly and easily deployed for a few dollars in total.

One limitation of ATtiny85/Digispark being small and inexpensive is that there is no hardware UART. This means no direct support for serial communications (RS232, 'com' ports on IBM compatible PC) which potentially limits interfacing to serial modules such as the HC-05 Bluetooth adapter and for 'simple' communication with PC for data capture, debugging etc.

Digispark provide the digiusb.exe application which can be used for two way communication with the ATtiny85/Digispark via the USB connection. However, this consumes a considerable amount of the limited memory available with the ATtiny85 and limits communication to using the digiusb.exe (possibly Ok for debugging purposes). Further, this approach does not help if trying to interface with serial modules such as the HC-05 Bluetooth adapter.

Even though the ATtiny85 does not include a hardware UART, there is a universal serial interface (USI) which can be used to implement equivalent functionality via software. There are a number of software serial libraries for the ATtiny85 that implement such UART support.

Various of these libraries are used to demonstrate serial communication between ATtiny85/Digispark for interfacing to serial modules and IBM compatible PC serial ports for sending/receiving information (particularly for debugging purposes). Since serial comm ports are largely superceded (i.e. not present) on current equipment, USB to TTL converter cables are used.

Details on how to setup the Arduino IDE and environment to programme ATtiny85 microcontrollers are given in the ATtiny85 introduction.


The Digispark board has a surface mount ATtiny85 with headers for all pins. Additionally, the board provides a 5V regulator for external DC power input and PCB copper traces that enable insertion into standard USB connector which can then be used for DC power source. There is an onboard LED to indicate power-on and an additional LED/resistor connected to pin1 for user control.

For convenience for this simple test, the USB power supply is used.

Again for convenience for this simple test, the ATtiny85 onboard oscillator is used rather than an external crystal source.

ISP programming is used as detailed in the ATtiny85 introduction.

Connection between the ATtiny85/Digispark and USB to TTL converter cables is given in the Schematics Section (also see the Photographs Section for physical demonstation).


Note: Image loading can be slow depending on server load.

  • ATtiny85 SchematicATtiny85 Schematic

    Silver Membership registration gives access to full resolution schematic diagrams.

    ATtiny85 Schematic

    ATtiny85 Schematic

  • ATtiny85 SchematicATtiny85 Schematic

    Silver Membership registration gives access to full resolution schematic diagrams.

    ATtiny85 Schematic

    ATtiny85 Schematic - single pin RX/TX

This project did not require a PCB.

The construction was done using prototyping board. See the photographs and schematic diagram sections.

Qty Schematic Part-Reference Value Notes
Resistors
1R110k1/4W, 10% 
Diodes
1D11N4001 
Integrated Circuits
1U1Digispark/ATtiny85microcontroller datasheet
1Q1BC547NPN small signal transistor
Miscellaeous
3J1wiresDupont pin jumper wires
1Z1USB-TTLUSB-TTL converter module
Description Downloads
Bill of Materials Text File Download

Before connecting the Digispark to the USB port, check for any shorts (direct positive voltage to ground connection) as this could damage both the Digispark board and the USB/PC.

ATtiny85 Two-way Serial Comms/SoftSerial.h Library

When installing Digispark boards as part of the Arduino IDE (see ATtiny85 introduction) the SoftSerial.h library for serial communications is available (2).

The SoftSerial library is exactly the same as the "standard" Arduino SoftwareSerial library but used with the TinyPinChange library which allows to share the "Pin Change Interrupt" Vector. This means for the ATtiny85 (Digispark), it's possible to declare the same pin for TX and RX (see following sub-section). Data direction is set by using the txMode() and rxMode() methods.

The Arduino sketch in Code Snippet 1 shows simple two-way serial communication between ATtiny85 and PC (with USB-TTL converter cable) using seperate TX and RX pins. Note that this example does not use a buffer to capture input received serial data, and so only echo's the first character of any received string (Code Snippet 2 shows simple use of a input buffer). This is potentially useful in a scenario such as when a single character denoting a command to be performed (via Switch statement or similar) is to be used.

Code Snippet 1:


#include  

#define rxPin 2
#define txPin 3
SoftSerial mySerial(rxPin, txPin); 

void setup(){
  //delay so can connect the port in the receiving software/terminal
  delay(3000); 
  mySerial.begin(9600); //set the data rate for the serial port
  mySerial.txMode();    //before sending a message, switch to txMode
  mySerial.println("Hello, world?");
  mySerial.rxMode();    //switch back to rxMode to wait for comms
}

void loop(){
  //simple receive single char at a time
  //if multiple char's sent, will only 'capture' the first
  
  if(mySerial.available()){
    mySerial.txMode();
    mySerial.print("\nReceived: ");
    mySerial.write(mySerial.read());
    mySerial.print("\n");
    mySerial.rxMode();
  }
}
   				

Code Snippet 2: Use of a buffer for input received serial data


#include  

#define BUFFER_LEN 12 
#define rxPin 2
#define txPin 3
SoftSerial mySerial(rxPin, txPin); 

signed int i=0;
char buf[BUFFER_LEN];

void setup(){
  //delay so can connect the port in the receiving software/terminal
  delay(3000); 
  mySerial.begin(9600); //set the data rate for the serial port
  mySerial.txMode();    //before sending a message, switch to txMode
  mySerial.println("Hello, world?");
  mySerial.rxMode();    //switch back to rxMode to wait for comms
}

void loop(){
  //receive chars into buffer, echo back when CR received
  if (mySerial.available()){
    buf[i]= mySerial.read();   
    if (int(buf[i])==13 || int(buf[i])==10 ){ //If Carriage return has been reached
      mySerial.txMode();
      mySerial.print(">");mySerial.print(buf);mySerial.println("<");
      mySerial.rxMode();
      for(int x=0;x<=BUFFER_LEN;x++){ //erase buffer
        buf[x]='\0';
      }
      i=-1;
    } 
    i++;
  } 
}                
                

ATtiny85 Two-way Serial Comms with single pin/SoftSerial.h Library

As mentioned in the previous section, advantage of the digispark SoftSerial library is the ability to declare the same pin for TX and RX. This requires some code changes as shown in Code Snippet 3. Also, the RX and TX pins of the USB-TTL converter need to be tied together (linked). This "loop back" of the USB-TTL converter pins means that any data (character string etc) received at the ATtiny85 pin will be immediately "echoed" back to the USB-TTL.

This "echo" on received data perhaps not a problem in the situation of using a terminal program (Arduino serial monitor for example) to send commands/data to the ATtiny85 (especially in debugging circumstances). Perhaps other custom software if being used to interact with the ATtiny85 could be programmed to ignore the "echo". In circumstances that the "echo" of data is not acceptable, the Schematic Section shows a simple circuit (just a single resistor, diode and NPN signal transistor) that can be used to eliminate the "echo" of data due to a single pin being used on the Attiny85 for serial communications.

Note that this is half-duplex communications, on a single wire, so obviously cannot expect the send and receive data at the same time. Application program to be "reliable" would require some form of data send and receive acknowledgement ("hand-shaking" protocol) to ensure in the correct state when expecting incoming data and or when ready to transmit. Although this is probably not required for debugging situations.

Code Snippet 3: ATtiny85 two-way comms. with single pin


#include  
#include  //important to include this for single pin rx/tx

#define BUFFER_LEN 12 
//#define rxPin 2
//#define txPin 3
//SoftSerial mySerial(rxPin, txPin); 
#define TX_RX_PIN  2 
SoftSerial mySerial(TX_RX_PIN, TX_RX_PIN); 

signed int i=0;
char buf[BUFFER_LEN];

void setup(){
  //delay so can connect the port in the receiving software/terminal
  delay(3000); 
  mySerial.begin(9600); //set the data rate for the serial port
  mySerial.txMode();    //before sending a message, switch to txMode
  mySerial.println("Hello, world?");
  mySerial.rxMode();    //switch back to rxMode to wait for comms
}

void loop(){
  //receive chars into buffer, echo back when CR received
  if (mySerial.available()){
    buf[i]= mySerial.read();   
    if (int(buf[i])==13 || int(buf[i])==10 ){ //If Carriage return has been reached
      mySerial.txMode();
      mySerial.print(">");mySerial.print(buf);mySerial.println("<");
      mySerial.rxMode();
      for(int x=0;x<=BUFFER_LEN;x++){ //erase buffer
        buf[x]='\0';
      }
      i=-1;
    } 
    i++;
  } 
}                
                

ATtiny85 transmit only/TinyDebugSerial.h Library

One limitation of ATtiny85/Digispark being physically small and inexpensive is that there is limited memory. The bootloader uses ~2K of the ~8K available, and using SoftSerial.h for two-way serial communications will occupy approximately half of the 6K left. In the case of debugging, transmitting only is a likely scenario and the TinyDebugSerial.h Library is an option that only takes ~1K of code space.

The TinyDebugSerial.h Library specifies the ATtiny85/Digispark pin PB2 as the transmit pin (which is then connected to the RX of the USB-TTL converter). The following code snippet shows an example of using the TinyDebugSerial.h Library.

The TinyDebugSerial.h library for serial communications is available (3).

Code Snippet 4: ATtiny85 transmit only


#include 
TinyDebugSerial mySerial = TinyDebugSerial();
//RX of USB-TTL goes to Digispark Pin PB2

unsigned int counter=0;

void setup(){
  mySerial.begin(9600);  // set the data rate for the SoftwareSerial port
  delay(3000); //give a delay so can connect the port in the receiving software
  mySerial.println("hello world");
}

void loop(){
    delay(1000);
    counter=counter+1;
    mySerial.print("="); mySerial.println(counter);
}                
                

The circuit is the basic minimum required to enable the ATtiny85 microcontroller to operate after being programmed with a suitable .hex file. So this circuit was only laid out on a breadboard.

Before connecting the Digispark to the USB port, check for any shorts (direct positive voltage to ground connection) as this could damage both the Digispark board and the USB/PC.

Comments/Questions

No comments yet.

Add Comment/Question

Only Logged-In Members can add comments

"If we could sell our experiences for what they cost us, we'd all be millionaires".

Please donate any amount to help with hosting this web site.

If you subscribe (only $2/annum) you can view the site without advertisements and get emails abouts updates etc.

X

Sorry!

Only logged-in users with Silver Membership and above can download.


Silver Membership is only $2, so join now!