Categories

Program an attiny2313 with an Arduino

This is the second part of setting up an Arduino to program an Attiny microcontroller, in the previous parts we added support for the variety of microcontrollers. For reference purposes this is what the Attiny2313 will be like when you do this, the pins and the corresponding Arduino pins are shown below, as you can see you still get 16 I/O pins, I2C capabilities, 3 PWM pins and a serial interface for communications.

 

attiny2313 arduino

attiny2313 arduino

You will need to add support for the ATtiny2313 as per Programming ATtiny85 with Arduino Uno but you need to add the following

http://drazzy.com/package_drazzy.com_index.json

 

Setup attiny2313 connection with Arduino board

Connect up your Attiny2313 to your Arduino as follows

1) attiny2313 pin 1 to arduino pin 10 reset
2) attiny 2313 pin 17 to arduino pin 11 MOSI
3) attiny2313 pin 18 to arduino pin 12 MISO
4) attiny2313 pin 19 to arduino pin 13 SCK
5) attiny2313 pin 10 to arduino GND
6) attiny2313 pin 20 to arduino VCC

Layout

The diagram does not show the LED we connected to test with, this was connected to pin 7.

Arduino-to-atttiny2313

Steps

Open the ArduinoISP program and upload the ArduinoISP program to the Arduino board to turn it into an ISP programmer.

1) Open ArduinoISP program.
File>Examples>ArduinoISP

2) Select the serial port.
Tools>Port>COMX (enter your com port here)

2) Select the required Arduino board, in this case we are using an Arduino Uno.
Tools>Board>Arduino Uno

3) Set the programmer type as AVRISP MKII.
Tools>Programmer>AVRISP MKII

4) Press the upload button.

5) Now your Arduino Uno is setup as the ISP programmer. Now to setup your Arduino to program your Attiny with a modified blink example

6) Select the AVR target for the Arduino ISP to program to.
Tools>Board>ATtinyx313

7) Change the Board for the Arduino ISP to program to.
Tools>Chip>ATtiny2313

8) Make sure the Clock for the Arduino ISP is set to 8Mhz.
Tools>Clock>8mhz(internal)

You can see these settings in the screenshot below

attinyx313

9) Set the programmer mode.
Tools>Programmer>Arduino as ISP

10) Finally, start the sketch upload to the Attiny2313 microcontroller
File>Upload

This is our code example to upload

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

 

If you connect an LED to pin 7 of your Attiny2313 you will see an LED flash on and off but it seems a bit slow

If you want you can Burn the Bootloader to the Attiny and the microcontroller will run at 8Mhz and the example above the led will flash on and off at the correct speed

FacebookTwitterGoogle+TumblrStumbleUponRedditLinkedInWhatsAppPinterestBaiduBufferDeliciousDiigoDiggOdnoklassnikiSina WeiboWordPressBlogger PostEmailShare

Programming ATtiny85 with Arduino Uno

The Attiny85 is a nice little 8 pin chip which is ideal for simple projects, they are low cost and if you have an Arduino you can actually burn the bootloader and upload your sketches to the microcontroller, this is perfect for beginners.

I recommend buying one of the cheap Arduino Uno clones for this task and using it for this purpose.

ATtiny45-85

To program the ATtiny85 we need to first set Arduino Uno as an ISP. Connect your Arduino Uno to the PC. Open the Arduino IDE and open the ArduinoISP example file (File -> Examples -> ArduinoISP) and upload it. If there are no error messages the your Arduino should be good to go.

By default the Arduino IDE doesn’t support ATtiny85 but luckily it is quite easy to add ATtiny board support to the Arduino IDE. to do this you simply goto Open File -> Preferences and in the Additional Boards Manager URLs add the following https://raw.githubusercontent.com/damellis/attiny/ide-1.6.x-boards-manager/package_damellis_attiny_index.json. Now go to  Tools -> Board -> Board Manager  scroll down the list to where it says “attiny by Davis A. Mellis”. Click on that and install it. and you should see the following

attiny boards manager

attiny boards manager

Navigate to the boards menu and you should have some new options, here I have highlighted the option required

attiny in boards manager

Now we will get ready to program our Attiny, we need some parts here as we will building a simple circuit. You can build this on a breadboard

Assembly List

Label Part Type Properties
C1 Electrolytic Capacitor capacitance 10µF; voltage 16V; package 100 mil [THT, electrolytic]
LED1 Red (633nm) LED color Red (633nm); leg yes; package 5 mm [THT]
Part1 Arduino Uno (Rev3) type Arduino UNO (Rev3)
R1 470Ω Resistor pin spacing 400 mil; bands 4; package THT; tolerance ±5%; resistance 470Ω
U1 ATTINY45 editable pin labels false; variant dip08; chip label attiny45; package dip

Layout

In this example we will connect the Attiny to our Arduino and program it, we will also connect an LED which we will use for test purposes

The Arduino to Attiny 85 connections are as follows

ATtiny Pin 2 to Arduino Pin 13
ATtiny Pin 1 to Arduino Pin 12
ATtiny Pin 0 to Arduino Pin 11
ATtiny Reset Pin to Arduino Pin 10

Lets see a breadboard layout of what we will be building

arduino and attiny85_bb

 

 

A 10uF capacitor is required between RESET and GND in arduino. This is to avoid the arduino from being auto reset when we upload the program to attiny85. If you are using a electrolytic capacitor make sure the anode of the capacitor goes in GND of the Arduino.

Coding and uploading sketches

Now we are ready to upload a code example to our Attiny. First we have to setup the Arduino IDE as follows

Select ATtiny under Tools -> Board.
Select ATtiny85 under Tools -> Processor.
Select 8 MHz (internal) under Tools -> Clock.
Select Arduino as ISP is selected under Tools -> Programmer.
Select Tools -> Burn Bootloader.

All going well you should see a success message

We then modify the Blink sketch to use pin 4 of the Attiny

void setup() {
  // initialize digital pin 4 as an output.
  pinMode(4, OUTPUT);
}
 
// the loop function runs over and over again forever
void loop() {
  digitalWrite(4, HIGH);   // turn the LED on (HIGH is the voltage level)
  delay(1000);              // wait for a second
  digitalWrite(4, LOW);    // turn the LED off by making the voltage LOW
  delay(1000);              // wait for a second
}

 

Upload the sketch and if there are no errors the LED connected to pin 3 of your Attiny85 should blink on and off and that’s it.

FacebookTwitterGoogle+TumblrStumbleUponRedditLinkedInWhatsAppPinterestBaiduBufferDeliciousDiigoDiggOdnoklassnikiSina WeiboWordPressBlogger PostEmailShare

Arduino and TMP36 example

The TMP35, TMP36, and TMP37 are low voltage, precision centigrade temperature sensors. They provide a voltage output that is linearly proportional to the Celsius (Centigrade) temperature.
The TMP35/TMP36/TMP37 do not require any external calibration to provide typical accuracies of ±1°C at +25°C and ±2°C over the –40°C to +125°C temperature range

The low output impedance of the TMP35/TMP36/TMP37 and its linear output and precise calibration simplify interfacing to temperature control circuitry and A/D converters. All three devices are intended for single-supply operation from 2.7 V to 5.5 V maximum.

tmp36 pinout

tmp36 pinout

 

Features:

  • Low Voltage Operation (+2.7 V to+5.5 V)
  • Calibrated Directly in °C
  • 10 mV/8°C Scale Factor (20 mV/8°C on TMP37)
  • ±2°C Accuracy OverTemperature
  • ±0.5°C Linearity
  • Specified -40 °C to +125 °C, Operation to +150 °C
  • Less than 50 µA Quiescent Current
  • Shutdown Current 0.5 µA max

 

Schematics

The TMP36 is very easy to connect to your Arduino, here is a schematic and breadboard layout

arduino and tmp36_schem

 

arduino and tmp36_bb

 

Code

int sensorPin = 0;
 
void setup()
{
  Serial.begin(9600);
}
 
void loop()
{
 
 int reading = analogRead(sensorPin);  
 // measure the 5v with a meter for an accurate value
 //In particular if your Arduino is USB powered
 float voltage = reading * 4.68; 
 voltage /= 1024.0; 
 
 // now print out the temperature
 float temperatureC = (voltage - 0.5) * 100;
 Serial.print(temperatureC); 
 Serial.println(" degrees C");
 
 delay(1000);
}

 

Testing

Open the serial monitor

22.67 degrees C
23.12 degrees C
24.50 degrees C
25.87 degrees C
26.78 degrees C
26.32 degrees C
26.32 degrees C
25.87 degrees C
25.41 degrees C

 

Link
5pcs/lot TMP36 TMP36GT9Z Low Voltage Precision Celsius temperature sensor

FacebookTwitterGoogle+TumblrStumbleUponRedditLinkedInWhatsAppPinterestBaiduBufferDeliciousDiigoDiggOdnoklassnikiSina WeiboWordPressBlogger PostEmailShare

Some temperature functions

In previous examples we have had various temperature sensors, most of these will display the humidity and the temperature in celsius but it is easy with a few calculations to add support for other useful data

these should be self explanatory

Code

//Celsius to Fahrenheit conversion
double Fahrenheit(double celsius)
{
return 1.8 * celsius + 32;
}

The kelvin is a unit of measure for temperature based upon an absolute scale. It is one of the seven base units in the International System of Units and is assigned the unit symbol K

//Celsius to Kelvin conversion
double Kelvin(double celsius)
{
return celsius + 273.15;
}

In simple terms, the dew point (dew point temperature or dewpoint) is the temperature at which a given concentration of water vapor in air will form dew. More specifically it is a measure of atmospheric moisture.

// dewPoint function NOAA
double dewPoint(double celsius, double humidity)
{
double A0= 373.15/(273.15 + celsius);
double SUM = -7.90298 * (A0-1);
SUM += 5.02808 * log10(A0);
SUM += -1.3816e-7 * (pow(10, (11.344*(1-1/A0)))-1) ;
SUM += 8.1328e-3 * (pow(10,(-3.49149*(A0-1)))-1) ;
SUM += log10(1013.246);
double VP = pow(10, SUM-3) * humidity;
double T = log(VP/0.61078); // temp var
return (241.88 * T) / (17.558-T);
}
// delta max = 0.6544 wrt dewPoint()
// 5x faster than dewPoint()
double dewPointFast(double celsius, double humidity)
{
double a = 17.271;
double b = 237.7;
double temp = (a * celsius) / (b + celsius) + log(humidity/100);
double Td = (b * temp) / (a - temp);
return Td;
}
FacebookTwitterGoogle+TumblrStumbleUponRedditLinkedInWhatsAppPinterestBaiduBufferDeliciousDiigoDiggOdnoklassnikiSina WeiboWordPressBlogger PostEmailShare