Digispark Basics

Date: 2016-06-09
categories: avr;

Contents

This page is mirrored from the original Digistump/Digispark documentation, and is preserved here in case the original is taken offline.

Differences (from the Arduino) and limitations

The Digispark is compatible with the Arduino IDE - that does not, however, mean it can do everything an Arduino can do. In order to achieve the low cost and small size of the Digispark some compromises had to be made.

The Arduino programming reference is here: http://arduino.cc/en/Reference/HomePage

Here is a list of some of the differences worth considering when designing or troubleshooting (pin differences are outlined in the sections below this list):

Digital Write:

void setup() {
    //All pins are capable of Digital output, though P5 is 3 V at HIGH instead of 5 V
    pinMode(0, OUTPUT); //0 is P0, 1 is P1, 2 is P2, etc. - unlike the analog inputs, for digital outputs the pin number matches.
}

void loop() {
    digitalWrite(0,HIGH); //Turn the pin HIGH (5 V)
    delay(1000);
    digitalWrite(0,LOW); //Turn the pin LOW (GND)
    delay(1000);
}

Digital Read:

NOTE: The internal pull-up resistor (turned on by calling digitalWrite(0) after setting the pin to output, where 0 is the pin number) are much weaker (about 25 kohm) on an ATtiny than on an Arduino, so the onboard LED interferes with them. If you need them, you can use a different port. Change your circuit to not need the internal pull-up, or cut the LED trace. For Model A this would apply to P1 for Model B this would apply to P0.

int sensorValue = 0;

void setup() {
    //All pins are capable of digital input.
    pinMode(0, INPUT); //0 is P0, 1 is P1, 2 is P2, etc. - unlike the analog inputs, for digital inputs the pin number matches.
}

void loop() {
    sensorValue = digitalRead(1); //Returns HIGH or LOW (true or false / 1 or 0).
}

Analog Read:

int sensorValue = 0;

void setup() {
    //You need not set pin mode for analogRead - though if you have set the pin to
    //output and later want to read from it then you need to set pinMode(0,INPUT);
    //where 0 is the physical pin number not the analog input number.
    //
    //See below for the proper pinMode statement to go with each analog read.
}

void loop() {
    // The analog pins are referenced by their analog port number, not their pin
    //number and are as follows:

    sensorValue = analogRead(1); //Read P2
    //To set to input: pinMode(2, INPUT);
    //THIS IS P2, P2 is analog input 1, so when you are using analog read, you refer to it as 1.

    //sensorValue = analogRead(2); //Read P4
    //To set to input: pinMode(4, INPUT);
    //THIS IS P4, P4 is analog input 2, so when you are using analog read, you refer to it as 2.

    //sensorValue = analogRead(3); //Read P3
    //To set to input: pinMode(3, INPUT);
    //THIS IS P3, P3 is analog input 3, so when you are using analog read, you refer to it as 3.

    //sensorValue = analogRead(0); //Read P5
    //To set to input: pinMode(5, INPUT);
    //THIS IS P5, P5 is analog input 0, so when you are using analog read, you refer to it as 0.
}

Analog Write: (AKA PWM)

void setup() {
    //P0, P1, and P4 are capable of hardware PWM (analogWrite).
    pinMode(0, OUTPUT); //0 is P0, 1 is P1, 4 is P4 - unlike the analog inputs, 
                        //for analog (PWM) outputs the pin number matches the port number.
}

void loop() {
    analogWrite(0,255); //Turn the pin on full (100%)
    delay(1000);
    analogWrite(0,128); //Turn the pin on half (50%)
    delay(1000);
    analogWrite(0,0);   //Turn the pin off (0%)
    delay(1000);
}