Warning: this document has to be updated by pointing to https://github.com/SpenceKonde/ATTinyCore
This note describes the configuration of an ATtiny85 based microcontroller development board named Digispark and similar to the Arduino line. It is available in many online marketplaces for roughly 1 dollar (e.g., Ebay, Amazon, AliExpress) and is shipped fully assembled, including a V-USB interface (a software-only implementation of a low-speed USB device for Atmel's AVR microcontrollers). Coding is similar to Arduino: it uses the familiar Arduino IDE and is already provided with a ready-to-use bootloader (fully integrated with Arduino), also allowing to be upgraded. Comparing it with the ATmega328P microcontroller, it is cheaper, smaller, and a bit less powerful. Digispark is copyrighted by Digistump LLC under the Creative Commons Attribution-ShareAlike 3.0.
The Arduino IDE needs to be installed first. Check Basic Arduino Installation and Install the Arduino Software (IDE) on Windows PCs for guided installation procedures.
Warning: instead of http://digistump.com/package_digistump_index.json, use https://github.com/SpenceKonde/ATTinyCore
A tutorial published at the Digispark wiki describes how to add the Digispark support to Arduino 1.6.X (1.6.4+) via the Arduino Boards Manager. This tutorial refers to the standard Digistump URL to be added to the Additional Boards Manager URLs: http://digistump.com/package_digistump_index.json. Anyway, at the moment of writing the easiest and most updated repository for the Digispark appears DigistumpArduino by ArminJo. Follow the installation section and replace https://raw.githubusercontent.com/ArminJo/DigistumpArduino/master/package_digistump_index.json instead of the previously mentioned URL http://digistump.com/package_digistump_index.json. This offers a ready-to-use environment and most of the configurations in the following sections, like editing boards.txt to set the upload maximum size, are automatically done by this set-up.
If an URL is already available in "Additional Boards Manager URLs" line (e.g., for esp8266 boards), more URLs can be added, separating them by a comma.
Notice the last message of the Preferences form: “More preferences can be edited directly in the file”. If clicking the file name, the related installation directory is opened (the digistump root directory can be found under the packages subdirectory).
To configure the Arduino IDE for the Digispark board, select your target device
Tools -> Boards -> Digispark (Default - 16.5MHz) (in case DigistumpArduino is used: Tools --> Boards --> Digistump AVR Boards --> Digispark)
Select your programmer
Tools -> Programmer -> Micronucleus
Digispark comes with an old bootloader version (generally 1.06), anyway allowing upgrade (to be done via Micronucleus, as described below).
Three different Windows drivers can be installed for supporting the Digispark board:
- the firmware upload driver, to upload Arduino sketches with the Arduino IDE via the V-USB virtual serial port included in the Micronucleus bootloader
- the Micronucleus driver, to upload the bootloader via micronucleus.exe (e.g., for bootloader upgrade or bootloader customization)
- optionally, DigiCDC - Digispark USB CDC Serial Library, requiring appropriate Arduino library as well as the V-USB driver on the PC.
The firmware upload driver is installed via the Arduino IDE and the Micronucleus bootloader upgrade driver can be set-up via https://github.com/digistump/DigistumpArduino.
To install the Micronucleus driver, expand DigistumpArduino-master.zip into DigistumpArduino-master. Select tools directory; expand micronucleus-2.0a4-win.zip, Select micronucleus-2.0a4.
Then run:
Install Digistump Drivers.bat
Check also the Driver installation in DigistumpArduino for updated information.
Micronucleus is a bootloader designed for AVR ATtiny microcontrollers; its latest version offers significant improvements over V1.x; upgrading the bootloader allows taking advantage of the benefits described in the Micronucleus README: mainly a significantly compact size.
Download https://github.com/micronucleus/micronucleus and expand it to a local Windows directory.
At the time of writing, the latest bootloader is Micronucleus V2.04; its file is /firmware/releases/t85_default.hex
The command to flash the bootloader is the following: https://github.com/micronucleus/micronucleus/tree/master/commandline/builds/Windows/micronucleus.exe
Notice that t85_default.hex bootloader cannot be loaded directly to the Digispark board, as it is too big.
The allow loading the bootloader, it needs to be converted into a compressed version; we will generate it with name upgrade.hex. Check micronucleus/micronucleus#22 for further notes.
To generate upgrade.hex with Windows, open a CMD at the micronucleus directory. Check that micronucleus.exe can be executed:
micronucleus /?
Connect the Digispark USB to the PC and check that the PC can recognize it by assigning a virtual USB port.
Check the version of the bootloader (notice that 1.6 means 1.06).
The old micronucleus version v1.11 already comes with the upgrade releases for this version (and with the related upgrade directory):
- micronucleus-1.11-entry_ext_reset.hex
- micronucleus-1.11-entry_jumper_pb0.hex
- micronucleus-1.11-entry_jumper_pb0.hex
- micronucleus-1.11-ledpb1.hex
- micronucleus-1.11.hex
The file bootloaderconfig.h describes these upgrade bootloader releases.
To load them:
micronucleus --run *<filename>*.hex
The default bootloader version 1.11 can be uploaded with the command
micronucleus --run micronucleus-1.11.hex
All option files are in the same directory. The default micronucleus-1.11.hex bootloader starts the device in firmware update option and keeps it waiting for software upload for 6 seconds, then runs the software. An interesting option allows initializing the Digispark to immediately run the software without the 6 seconds delay; the software upload can be accomplished via the reset pin:
micronucleus --run micronucleus-1.11-entry-ext-reset-upgrade.hex
Coming back to the last bootloader version, as mentioned, the reference file t85_default.hex (micronucleus-master/firmware/releases/t85_default.hex) cannot be directly uploaded as too big. Example:
micronucleus --run t85_default.hex
It should fail with an error like: "Program file is 1602 bytes too big for the bootloader!"
In order to compress the bootloader to allow flashing the microcontroller, it has to be recompiled.
The upgrade directory is needed. As it is not included in the latest version, it needs to be downloaded from the v.1.11 old release:
Download https://github.com/micronucleus/micronucleus/tree/v1.11
Expand the zip file, select the upgrade directory and copy it to micronucleus-master\upgrade
Also copy the ruby directory to micronucleus-master\ruby
Keep all directory tree.
Then open a CMD to micronucleus-master:
cd upgrade
gem install libusb
ruby generate-data.rb t85_default.hex
It generates bootloader_data.c in the upgrade directory.
To compile it, the AVR compiler needs to be set in the path; it generally can be found at C:\Program Files\Arduino\hardware\tools\avr\bin.
Open a Cygwin Bash to the upgrade directory
export PATH=$PATH:/cygdrive/c/Program\ Files/Arduino/hardware/tools/avr/bin
make clean
make
Return to the CMD; run the following:
micronucleus --run upgrade.hex
The bootloader upgrade should succeed.
Check now the bootloader version:
micronucleus -?
Device should have bootloader version 2.3 (or newer).
ArminJo implemented a fork for the micronucleus repository of the digispark firmware with detailed documentation of installation and configuration options and many precompiled and ready-to-install firmware releases. Check Update the bootloader section in DigistumpArduino for information on how to install the bootloader. You can find there most of the bootloader upgrade configurations covering almost any needs, already prepared for upgrade installation; this generally avoids the need to generate a specific bootloader hex file.
Open cygwin to the firmware directory (it should include a Makefile and the configuration directory). Run
export PATH=$PATH:/cygdrive/c/Program\ Files/Arduino/hardware/tools/avr/bin
Note: to revise the configuration: firmware\configuration\t85_default\bootloaderconfig.h. This file includes comment notes describing the allowed configuration settings. For instance, edit:
#define AUTO_EXIT_NO_USB_MS 1
Then run the following:
make clean
make CONFIG=t85_default
A file named main.hex is generated
Move main.hex to the upgrade directory
You can then run ruby generate-data.rb main.hex; this can be also achieved with Windows: with a CMD run the following:
ruby generate-data.rb main.hex
Return to the cygwin shell; run the following:
cd ../upgrade
make clean
make
return to the CMD; run the following:
micronucleus --run upgrade.hex
Example:
..\commandline\builds\Windows\micronucleus.exe --run upgrade.hex
Notice that the Arduino IDE configuration has to be modified in order to change the maximum sketch size allowed to be upload.
Open a CMD
Insert the Digispark to the PC USB. Run the following:
micronucleus /?
Check the max size of the executable program.
Open the directory mentioned in the last message of the Preferences form (Arduino IDE, File, Preferences): “More preferences can be edited directly in the file”. If clicking the file name, the related installation directory is opened. Move to the packages\digistump\hardware\avr<version> subdirectory.
Edit boards.txt with a UNIX editor. Set digispark-tiny.upload.maximum_size to the value returned by micronucleus /?. Example:
digispark-tiny.upload.maximum_size=6520
Similarily to the latest bootloader version previously described, also version v1.11 can be configured and generated. Even if the hex files are ready available at the firmware/releases directory, the following procedure describes how to produce them.
Download micronucleus version v1.11.
It comes with the upgrade directory.
Edit micronucleus/firmware/bootloaderconfig.h
For instance, set:
#define AUTO_EXIT_NO_USB_MS 1
Open a cygwin bash to the micronucleus/firmware directory (it should include a Makefile)
export PATH=$PATH:/cygdrive/c/Program\ Files/Arduino/hardware/tools/avr/bin
make clean
make
A file named main.hex is generated
Move main.hex to the upgrade directory
You can then run ruby generate-data.rb main.hex; this can be also achieved with Windows: with a Windows CMD run the following:
cd upgrade
gem install libusb
ruby generate-data.rb main.hex
Return to the cygwin shell; run the following:
cd ../upgrade
make clean
make
return to the CMD; run the following:
micronucleus --run upgrade.hex
Example:
..\commandline\builds\Windows\micronucleus.exe --run upgrade.hex
The bootloader is flashed.
A valid schematic diagram for the Digispark module is at this url: http://www.electroschematics.com/wp-content/uploads/2015/08/Circuit-Diagram-of-the-ATtiny85-USB-Mini-Development-Board.png
The diode between the USB and 5V shall have the cathode (+) connected to +5V and the anode (–) connected with USB V+. The Digispark schematic diagram incorrectly inverts this diode.
Pinout:
- https://neonaut.neocities.org/images/digispark-pinout.png
- https://camo.githubusercontent.com/776b358cb7412939e03a65990fb36d0aa39e890f/687474703a2f2f6472617a7a792e636f6d2f652f696d672f50696e6f75745438352e706e67
- https://github.com/SpenceKonde/ATTinyCore/blob/master/avr/extras/ATtiny_x5.md
- https://github.com/ArminJo/DigistumpArduino#attiny85-on-digispark
Digispark pin numbering from 0 to 5:
- Digispark Pin 1 is generally the internal LED (ATTINY85 pin 6, PB1)
- Digispark Pin 3 is USB D- with pullup on Digispark board (ATTINY85 pin 2, PB3)
- Digispark Pin 4 is USB D+ (ATTINY85 pin 3, PB4)
- Digispark Pin 5 is the RESET (ATTINY85 pin 1, PB5)
- Digispark Pins 0 and 2 are just wired to ATTINY85 pins 5 (PB0) and 7 (PB2).
The power consumption of the Digispark board is influenced by:
- the microcontroller (which support SLEEP mode);
- the power LED (which can be removed);
- the 78L05 linear regulator (which can be removed);
- the LED connected to PB1 (only when the related output is set to HIGH);
- the USB circuit including Zener LED (current permanently flowing between +5V, the D- pull-up resistor and the zener diode). This can be eliminated by disconnecting the D- pull-up resistor from +5V and connecting it to USB V+ instead; in this case, the power consumption only happens when the USB is connected.
The Digispark ATtiny85 device has to be modified as follows to save power:
- remove the power led;
- remove the linear voltage regulator of the Digispark (LM78L05 has a quiescent current of about 2.5 to 3 milliamperes) and in case substitute it with diodes in series (or zener) to reduce the voltage to about 3.2V (this contributes to reduce quiescent current to the minimum);
- enable D- pull-up only when the USB is connected (it can be achieved by moving the pull-up resistor connection to take the positive voltage directly from the USB +5V and not from the Digispark internal +5V, which is after the schottky diode separating USB from the internal +5V; this configuration reduces power consumption by avoiding current flow throgh the D- zener during normal operation and in parallel preserving the D- pull-up when USB is connected, which is anyway needed by the USB hw interface);
Notice that after the modification of the D- pull-up resistor, the Digispark bootloader has to be flashed with "#define ENTRYMODE ENTRY_EXT_RESET" in micronucleus-master/firmware/configuration/t85_default/bootloaderconfig.h; then use the Digispark reset pin to enable the firmware upload. Setting ENTRYMODE to ENTRY_EXT_RESET avoids to hang the Digispark if D- is not pulled-up. A micro-switch between D5 and ground is appropriate, also pulling up D5 to VCC with a 22kohm resistor.
In case the Digispark freezes with no possibility to perform any upload, the micronucleus has to be flashed again. You need an AVR USB ISP programmer to do this, or simply an Arduino with ArduinoISP sketch that allows emulating a STK500v1 programmer.
This procedure shows how to flash the micronucleus with an Arduino and can also be used to install the micronucleus to a new ATTINY85 chip.
The following is the hardware connection between Digispark and Arduino Uno (or Arduino Nano, which has the same pins): just 4 wires.
Arduino | Digispark | (ATTINY85 description) | |
---|---|---|---|
D10 | --> | P5 | RESET |
D11 | --> | P0 | SPI MOSI |
D12 | --> | P1 | SPI MISO |
D13 | --> | P2 | SPI SCK |
The Digispark device can be powered via USB.
Notes:
- Digispark pins P3 and P4 are not used.
- Usage of a capacitor (mentioned in some descriptions) is not needed.
- LEDs (controlled by the sketch) are not strictly necessary.
- 5V and GND wires to power the Digispark board are not needed (as mentioned, the Digispark can be powered via USB).
Start the Arduino IDE and upload the ArduinoISP sketch to the Arduino Uno (or Nano).
- Verify that the IDE configuration is appropriate to upload sketches to the Arduino board
- Open the ArduinoISP example sketch (check the IDE menu): File > Examples > ArduinoISP > ArduinoISP
- Upload the sketch to Arduino.
The following setting will allow reading the avrdude command line, subsequently used to upload the micronucleus hex code.
File > Preferences. Select "Show verbose output during" "upload"
Install ATTinyCore from the IDE boards manager.
Install the ATtiny boards from the IDE boards manager.
- File > Preferences > Additional Boards Manager URLs
- Add http://drazzy.com/package_drazzy.com_index.json
https://raw.githubusercontent.com/damellis/attiny/ide-1.6.x-boards-manager/package_damellis_attiny_index.jsonto the configuration. If additional urls are available, use a comma to separate the new URL from the existing ones. - Click the OK button to save your updated preferences.
- Open the boards manager: Tools > Board > Boards Manager...
- Search attinycore by Spence Konde
attiny and load "attiny by David A. Mellis".
Set up the IDE configuration to use ArduinoISP:
- Tools > Chip: ATTiny85
- Tools > Clock > Set to the correct CPU clock (e.g., Internal 16.5 Mhz, or Internal 8 MHz, or Internal 1 MHz)
- Tools > Board > ATtiny Microcontrollers > Attiny25/45/85- Tools > Processor > ATtiny85- Tools > Clock > Set to the correct CPU clock (e.g., Internal 16 Mhz, or Internal 8 MHz, or Internal 1 MHz) - Tools > Port > Select the appropriate Arduino Port
- Tools > Programmer > Arduino as ISP
After installing the ArduinoISP sketch and connecting the devices, the firmware upload can be done at any time, because the ISP sketch automatically drives the Digispark device.
Select any sample sketch, like Blink:
Examples > Basics > Blink
Compile and upload the sketch.
When uploading, the IDE output log shows the full AVRDUDE command used to load the sketch, including command line arguments and paths:
<path>/avrdude -C<path>/avrdude.conf -v -pattiny85 -cstk500v1 -P<com port> -b19200 -Uflash:w:<path>/sample.ino.hex:i
Copy the command, open a CMD and paste the command to the command prompt. Modify it as follows.
Reading fuses (example of <com port>
: COM6):
<path>/avrdude -C<path>/avrdude.conf -v -pattiny85 -cstk500v1 -P<com port> -b19200 -U lfuse:r:-:i
Note: for normal operation, fuses should be E:FE, H:DF, L:F1
.
To download the micronucleus firmware, select releases (and not upgrades): https://github.com/ArminJo/micronucleus-firmware/tree/master/firmware/releases
Flash micronucleus and update fuses (example of <com port>
: COM6):
<path>/avrdude -C<path>/avrdude.conf -v -pattiny85 -cstk500v1 -P<com port> -b19200 -Uflash:w:<path>\t85_default.hex:i -U lfuse:w:0xf1:m -U hfuse:w:0xdf:m -U efuse:w:0xfe:m
Now, if needed, micronucleus can be upgraded (example):
micronucleus.exe /run upgrade-t85_entry_on_power_on_no_pullup_fast_exit_on_no_USB.hex
Changing the CKSEL fuses from 16,5 MHz (-U lfuse:w:0xf1:m
) to 8 MHz internal clock (-U lfuse:w:0xE2:m
) and viceversa can be done at any time with avrdude and SPI hw configuration, but the micronucelus will not work at 8 MHz (minimum clock for V-USB is 12 MHz): you first need to revert the clock speed to 16,5 MHz to restore using the micronucelus.
The ATtiny85 is only specified to run at 16 MHz when powered at 5V, however you may be able to overclock it at 16 MHz with 3V (8 MHz is recommended for 3V and 8 or 16MHz with 5V).
ATtiny85 does not provide a native serial interface, but it can be generated via software:
Both provide a TTL UART hardware interface (using one or two pins different from P3 and P4, used for USB). If the PC has a physical serial port, an external TTL-to-RS232 hardware interface is needed. If USB is required to connect a PC, an external UART-USB hardware converter is needed.
The above described options are ways to monitor a Digispark via Arduino "serial monitor".
Similarly to the UART serial port, USB monitoring can be emulated via a bit-bang software implementation of the Digispark USB interface, emulating a serial port. All these options do not allow monitoring via Arduino "serial monitor".
-
DigiCDC, based on the V-USB project, needs appropriate Windows PC driver; it uses a software interrupt and needs no other timers or interrupts to be enabled in parallel to correctly work. Besides, to avoid USB problems, DigiCDC shall be started just after the microcontroller boot and shall be kept always active. It is slow and if you don't call a DigiCDC function (write, print, read, available, etc) every 10ms or less then you must throw in some DigiCDC.refresh(); for the USB to be kept alive - also replace your delays (i.e., "delay(100);") with DigiCDC.delays (i.e., "DigiCDC.delay(100);").
-
In order to to limit the code size, the suggested method for debugging and monitoring is emulating an USB keyboard through the DKeyboard library. No software driver is needed for the PC. It is suggested to include all monitoring code within related #define, so that it can be disabled for normal operation, when not needed. Avoid using punctuation and symbols in text strings (use just letters and numbers).
DKeyboard example:
#define USE_KEYBOARD
#define KEYBOARD_DELAY 1500 // Time to wait for the PC keyboard driver to become ready
#ifdef USE_KEYBOARD
#include "DKeyboard.h" // Use a modified tiny DigisparkKeyboard library
DKeyboardDevice Db = DKeyboardDevice(); // instantiate the Keyboard driver
#endif
void setup() {
...
#ifdef USE_KEYBOARD
Db.sendKeyStroke(0);
Db.delay(KEYBOARD_DELAY); // This is needed to wait for the PC keyboard driver to become ready
Db.sendKeyStroke(0); // This will ensure no character will be lost
#endif
...
}
void loop() {
...
#ifdef USE_KEYBOARD
Db.print("Value result is ");
Db.println(value);
Db.print("Hex value is ");
Db.println(hexvalue, HEX);
Db.delay(300); // ensure that the transmission is performed before the interrupt is turned off
#endif
...
}
Steps to use DKeyboard for debug log monitoring:
- Create a new folder called DKeyboard under the folder named "libraries" in your Arduino sketchbook folder. Create the folder "libraries" in case it does not exist yet. Copy all the files under libraries/DKeyboard to the target "DKeyboard" subfolder. This is a modified version of DKeyboard that slightly reduces its size so that it can be loaded to the Digispark ATTINY85 device. ATtiny85 presents itself to the PC as a keyboard USB HID.
- Connect the Digispark to the PC via USB
- Compile and load. If getting "operator '*' has no left operand" in "usbdrv.h:662:33", with an editor open usbdrv.h in the Arduino libraries directory (e.g., Arduino\libraries\DKeyboard\usbdrv.h) and replace
define USB_INTR_PENDING GIFR
with any value above 0x40, likedefine USB_INTR_PENDING 0x41
. - Immediately after the code is successfully uploaded, remove the Digispark USB from the PC (to prevent interference of the emulated keyboard)
- Open a Notepad and put the Windows focus there (maximizing the Notepad window is also suggested to prevent accidentally losing the focus)
- Insert the USB to the PC (do not move the focus out of the Notepad window until the USB is connected)
False errors of missing USB driver messages can occur with Windows (should be ignored). If no data is shown, increase KEYBOARD_DELAY timer.
The following DKeyboard Hello World code takes 2784 bytes (42% of the available rooms):
#define KEYBOARD_DELAY 1500
#include "DKeyboard.h"
DKeyboardDevice Db = DKeyboardDevice();
void setup() {
Db.sendKeyStroke(0);
Db.delay(KEYBOARD_DELAY);
Db.sendKeyStroke(0);
}
void loop() {
Db.println("Hello World");
Db.delay(300);
}
DKeyboard needs timer interrupt disabled. If timer interrupt is used (e.g, IRMP or IRemote, interrupts shall be temporarily disabled.
The following sample explains how to enter sleep mode with the lowest power level. It puts the processor in quiescent mode and uses Pin 0 (PB0) to resume from sleep (other pins can be added: PB0=PCINT0, PB1=PCINT1, PB2=PCINT2, PB3=PCINT3, PB4=PCINT4, PB5=PCINT5). PCINT0_vect is a single ISR vector for Pin Change interrupts related to all pins; use EMPTY_INTERRUPT(PCINT0_vect)
macro if no ISR vector is needed; otherwise use ISR(PCINT0_vect) {...}
.
#include <avr/sleep.h>
#include <avr/interrupt.h>
void sleep() {
#if defined (__AVR_ATtiny85__)
cli(); // Disable interrupts to set the sleep configuration
GIMSK |= _BV(PCIE); // Enable Pin Change Interrupts for all pins
PCMSK |= _BV(PCINT0); // Unmask Pin Change Interrupt for pin PB0 (=PCINT0)
ADCSRA &= ~_BV(ADEN); // ADC off (save power when sleeping)
set_sleep_mode(SLEEP_MODE_PWR_DOWN); // set sleep to the lowest power mode
sleep_enable(); // Sets the Sleep Enable bit in the MCUCR Register (SE BIT)
sei(); // Enable interrupts before sleeping
sleep_cpu(); // sleep
cli(); // Disable interrupts after resuming from sleep
PCMSK &= ~_BV(PCINT0); // Mask Pin Change Interrupt for pin PB0 (=PCINT0)
GIMSK &= ~_BV(PCIE); // Turn off Pin Change Interrupts for all pins
sleep_disable(); // Clear SE bit
ADCSRA |= _BV(ADEN); // Restore ADC on
sei(); // Enable interrupts again
#endif
}
#if defined (__AVR_ATtiny85__)
EMPTY_INTERRUPT(PCINT0_vect) // Null Pin Change Interrupt vector for all unmasked pins
/* Alternatively to EMPTY_INTERRUPT():
ISR(PCINT0_vect) {
// This is called when the interrupt occurs in case of Pin Change of all unmasked pins
}
*/
#endif
Consider setting the clock speed to 8 MHz internal clock (-U lfuse:w:0xE2:m
) when waking up the device from sleep mode through an interrupt in order to speed-up the wake-up time (4 to 5 ms from Power Down sleep with 16,5 MHz; 6 clocks with 8 MHz).
P5 (PB5) is the reset pin. Depending on the Digispark hardware, the reset function can be enabled or disabled. The default hardware implementation should have it disabled; in this case, bootloaders can be upgraded (the Micronucleus bootloader works and sketches can be uploaded using the bootloader), but ISP programming is disabled (as ISP connection needs the enabled RESET function; notice that ISP programming can be done via ISP device or via an appropriately confugured Arduino or Digispark).
Disabling the reset-function can be done through the RSTDISBL fuse via either ISP or HVSP programming (without High-voltage Serial Programmer), but cannot be done via Micronucleus bootloader. Enabling the reset needs an High-voltage Serial Programmer and cannot be done via ISP device (or Micronucleus bootloader).
When reset is disabled, you get a full application pin. If reset is enabled, pin 5 resets the microcontroller if the voltage is lower than around 2.5V. Anyway this pin can still be used as input with analogRead(0) when staying over 3V. analogRead(0) values between 620 and 1023 are generally safe to be used for applications without generating unwanted resets. Notice that 0 is the pin number to read the reset pin via analogRead.
The reset pin has a 30 to 60 kOhm internal pull-up resistor and the default value read by analogRead(0) is a bit lower than 1023 (empirically, 984 to 986, meaning about 96%). Reading 1023 can be done through an external pull-up.
The analog pins are referenced by their analog port number, not the Digispark pins:
analogRead(1); // Read Digispark pin P2
analogRead(2); // Read Digispark pin P4
analogRead(3); // Read Digispark pin P3
analogRead(0); // Read Digispark pin P5
Depending on the hardware, the internal Digispark LED is connected to P1 in model A (most used), or on P0 in model B.
Thanks, I updated the document.