I thought that I could use an atTiny85 as a lipo low voltage reporter.
Using ArduinoUno as ISP and first sketch for atTiny
I used the Arduino Uno as an avrisp to program the atTiny85 following the guide in
http://highlowtech.org/?p=1695. Note that before using the Uno as a programmer you need to install the sketch ArduinoIsp on itself. Install for this step you set the board to ArduinoUno. After that load any sketch e.g blink with pin13 changed to 0 , set the board and processor to attiny85.
Another great tutorial is at
https://www.hackster.io/arjun/programming-attiny85-with-arduino-uno-afb829
Serial Communications
Note that to monitor rx,tx either connect the pins 2 and 4 to tx, rx on an ArduinoUno, after downloading blink sketch to return to normal behaviour or use a usb serial adapter. I find that if using the Arduino you need to connect vcc to 3.3 v otherwise the serial communication is noisy.
Reading a voltage
val = analogRead(1); // read from ADC1 - pin7
val = analogRead(2); // read from ADC2 - pin3
val = analogRead(3); // read from ADC3 - pin2
Note that you cannot use the same physical pin for analogRead and SoftwareSerial.
The result is 0 to 1023 with 1023 representing 100% of vcc. So using Vcc of 3.3 and applying 1.1v to the adc the value of val is 342.
Using the internal voltage reference
See the following links:
http://www.re-innovation.co.uk/web12/index.php/en/information/electronics-information/accurate-voltage-measurement
To read using the internal 1.1v reference simply add :
analogReference(INTERNAL1V1);
to the setup function of the sketch.
However the actual internal voltage can be off by 10%.
My current atTiny85 returns 490 when measuring 0.5v that means Vinternal is 1023/490 * 0.5 = 1.04 rather than 1.1. Now you could write the actual internal voltage into a chip specific sketch or save the value in the EPROM for other sketches to read.
Fuses and preserving EEPROM and system clock
fusecalc
use of eeprom
Also you need to make sure the ATTiny is set to preserve EEPROM during upload with the ISP, this is done with the fuse settings. You need to look for a tutorial on fuse calculators for the AVRs. To set the EESAVE
you need to set the High fuse to 0xD7
, you can change this in the boards.txt
file. Here is a fuse calculator.
From Atmel-2586-AVR-8-bit-Microcontroller-ATtiny25-ATtiny45-ATtiny85_Datasheet.pdf
The CKDIV8 Fuse determines the initial value of the CLKPS bits. If CKDIV8 is unprogrammed, the CLKPS bits will be reset to “0000”. If CKDIV8 is programmed, CLKPS bits are reset to “0011”, giving a division factor of eight at start up.
The device is shipped with CKSEL = “0010”, SUT = “10”, and CKDIV8 programmed. The default clock source setting is therefore the Internal RC Oscillator running at 8 MHz with longest start-up time and an initial system clock prescaling of 8, resulting in 1.0 MHz system clock.
As CPU speed is not an issue I will use internal 1Mhz rather then 8MHz. This is done by programming CKDIV8 which means setting top bit of lfuse to 0. The internal oscillator of 8Mhz is selected by setting the CKSEL bits to 0010 which is the default.
Reading and Writing the EEPROM using AVRDUDE
First cd to the folder containing avrdude.exe:
cd C:\Users\edwin\AppData\Local\Arduino15\packages\arduino\tools\avrdude\6.3.0-arduino6/bin
c:
To read the eeprom to file eedump.hex:
avrdude -C../etc/avrdude.conf -v -pattiny85 -cstk500v1 -PCOM5 -b19200 \
-Ueeprom:r:eedump.hex:i
To write the contents of eedump.hex to eeprom:
avrdude -C../etc/avrdude.conf -v -pattiny85 -cstk500v1 -PCOM5 -b19200 \
-Ueeprom:w:eedump.hex:i