Saturday 29 October 2016

Adding OTA capability to my robot sketch

Follow the guide here
If you need to remove an old version of Arduino before installing the required version make sure to restart your pc before using the new version.

Output  from Arduino IDE on request to upload:
Sketch uses 256,387 bytes (24%) of program storage space. Maximum is 1,044,464 bytes.
Global variables use 36,264 bytes (44%) of dynamic memory, leaving 45,656 bytes for local variables. Maximum is 81,920 bytes.
python.exe C:\Users\edwin\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\2.3.0/tools/espota.py -i 192.168.1.95 -p 8266 --auth= -f C:\Users\edwin\AppData\Local\Temp\arduino_build_442534/esp8266_robot.ino.bin
16:19:05 [ERROR]: No Answer
python.exe C:\Users\edwin\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\2.3.0/tools/espota.py -i 192.168.1.95 -p 8266 --auth= -f C:\Users\edwin\AppData\Local\Temp\arduino_build_442534/esp8266_robot.ino.bin
Authenticating...FAIL
16:19:09 [ERROR]: Authentication Failed
python.exe C:\Users\edwin\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\2.3.0/tools/espota.py -i 192.168.1.95 -p 8266 --auth=123 -f C:\Users\edwin\AppData\Local\Temp\arduino_build_442534/esp8266_robot.ino.bin
Authenticating...OK
Uploading...................................................................................................................................................................................

Output on com port during upload:
lpls$|nd.......
WiFi connected
Server started
192.168.1.95
Start
Progress: 100%
End
 ets Jan  8 2013,rst cause:2, boot mode:(3,7)
load 0x4010f000, len 1384, room 16
tail 8
chksum 0x2d
csum 0x2d
v3de0c112
@cp:0
     ld
       .......
WiFi connected
Server started
192.168.1.95

Thursday 13 October 2016

First exploration of atTiny85

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


Pinouts - thanks to https://engineerzero.wordpress.com/2013/05/07/attiny-4585-pin-conventions-for-arduino-ide-sketches/

Serial Communications

atTiny85 does not have dedicated serial comms capability but serial comms can be done using SoftwareSerial. See http://www.instructables.com/id/ATtiny85-ATtiny84-Analog-Pins-Serial-Communication/step3/Using-SoftwareSerial-for-Communication/

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