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);
}
Only Logged-In Members can add comments