Categories

Digispark LED example

In this example I decided to take a look at one of the Digispark clone boards that are available. These boards are based around the ATtiny85 which have the micronucleus bootloader installed, this means you can then use the Arduino IDE as usual

There are instructions for setting up your Arduino IDE at http://digistump.com/wiki/digispark/tutorials/connecting, rather than repeating everything I recommend reading the steps there

In the Arduino “Board Manager”. That is “Digispark (Default – 16.5mhz)” under “Tools → Board”

When you have a program ready to upload you need to disconnect the board from the USB cable! Then press the upload button in the Arduino IDE interface and you will be asked to connect the board in the next 60 seconds. After you connect the digispark board don’t press anything and justwait until the code is uploaded.

Schematic

Digispark LED schematic

Digispark LED schematic

Code

This is a simple flash an LED example, the LED is connected to pin 1 in my example

#define Ledpin 1 // Select the pin where the led is attached
 
void setup() 
{ 
pinMode(Ledpin, OUTPUT); // initialize the digital pin as an output.
digitalWrite(Ledpin, LOW); // Set it in Low state
}
 
 
void loop() 
{
Toggle();
delay(500);
}
 
 
void Toggle(void)
{
if(digitalRead(Ledpin)==HIGH)
{ 
digitalWrite(Ledpin, LOW); 
}
else
{ 
digitalWrite(Ledpin, HIGH); 
}
}

 

Links
Digispark Kickstarter ATTINY85 General Micro USB Development Board

Digispark Pro kickstarter development board use Micro ATTINY167 module for Arduino

Programming an Attiny85 using an Arduino

In this part we will now try and program an Attiny85 microcontroller using an Arduino, the process is similar to our Attiny2313 example. Basically slightly different connections and change the chip being programmed in the IDE.

Connections

ATtiny85

ATtiny85

  • ATtiny Pin 7 to Arduino Pin 13 (SCK)
  • ATtiny Pin 6 to Arduino Pin 12 (MISO)
  • ATtiny Pin 5 to Arduino Pin 11 (MOSI)
  • ATtiny Pin 1 to Arduino Pin 10 (RESET)

Layout

Arduino to atttiny85

Arduino to atttiny85

Schematic

Arduino to atttiny85 schematic

Arduino to atttiny85 schematic

You will need to setup your Arduino, here is a screenshot of my settings

attiny85 setup

attiny85 setup

Code

Modify the basic blink LED sketch to use Pin 5 (D0)

// the setup function runs once when you press reset or power the board
void setup() 
{
  // initialize digital pin 0 as an output.
  // Attint85 pin 5
  pinMode(0, OUTPUT);
}
 
// the loop function runs over and over again forever
void loop() 
{
  digitalWrite(0, HIGH);   // turn the LED on (HIGH is the voltage level)
  delay(1000);              // wait for a second
  digitalWrite(0, LOW);    // turn the LED off by making the voltage LOW
  delay(1000);              // wait for a second
}

Attiny85 LDR and LED – arduino version

In this example we will connect an LDR to an Attiny85, once the value read drops below a certain value we will switch an LED on.

For this example we used the Arduino IDE to write our code. We’ll post a different version using Atmel Studio or equivalent soon.

Schematic

LDR and LED

LDR and LED

Code

int analogPin = 3; //pin 2
int LEDPin = 1; //pin 6
int val = 0;

void setup()
{
  pinMode(LEDPin, OUTPUT);
  digitalWrite(LEDPin,LOW);
  delay(2000);
  digitalWrite(LEDPin,HIGH);
}

void loop()
{
  val = analogRead(analogPin);    // read the input pin
  if(val < 200)
  {
    digitalWrite(LEDPin,HIGH);
    delay(200);
  }
  else
  {
    digitalWrite(LEDPin,LOW);
  }
}

Links

ATTINY85 – flash multiple LEDs

Similar to our previous example, in this case we connect 3 LEDs up to Pin 5, 6 and 7 which are PB0,1 and 2

Here is the schematic for this example

Schematic

attiny85 three LEDs

attiny85 three LEDs

Source


#include <avr/io.h>
#define F_CPU 1000000UL
#include <util/delay.h>
int main (void)
{
// port B pins 0, 1, 2 as output
DDRB = (1 << PB2) | (1 << PB1) | (1 << PB0);

unsigned int i = 0;
while(1)
{
PORTB = i & ( (1 << PB2) | (1 << PB1) | (1 << PB0) );

i = (i+1) % 8;

_delay_ms( 500 );
}
}

Attiny85 beginner example – Flash an LED

The hello world of examples this time for an ATTINY85, although this should work with similar chips as well. We will connect the LED to pin 2 (PB3) and simply flash it on and off.

Here is our schematic

Schematic

attiny85 Basic LED

Code

The code was written in Atmel Studio

#include <avr/io.h>
#define F_CPU 1000000UL
#include &lt;util/delay.h&gt;

// Define the I/O port to be used for the LED.
#define LED_PORT PB3

int main(void) {

// Set the LED port number as output.
DDRB |= (1 &lt;&lt; LED_PORT);

// Start infinite loop.
while (1)
{

// Set the LED bit to "1" - LED will be "on".
PORTB |= (1 &lt;&lt; LED_PORT);
// The delay function simply does N-number of "empty" loops.
_delay_ms(200);
// Set the LED bit to "0" - LED will be "off".
PORTB &amp;= ~(1 &lt;&lt; LED_PORT);
_delay_ms(400);
}

return (0);
}

And another way

#include <avr/io.h>
#define F_CPU 1000000UL
#include <util/delay.h>

// Define the I/O port to be used for the LED.
#define LED_PORT PB3

int main(void) {

 // Set the LED port number as output.
 DDRB |= (1 << LED_PORT);

 // Start infinite loop.
 while (1)
 {

 // Set the LED bit to "1" - LED will be "on".
 PORTB |= (1 << LED_PORT);
 // The delay function simply does N-number of "empty" loops.
 _delay_ms(200);
 // Set the LED bit to "0" - LED will be "off".
 PORTB &= ~(1 << LED_PORT);
 _delay_ms(400);
 }

 return (0);
}