# Arduino #
Home page:
Arduinos generally use the Atmel ATmega CPU chip, but there are variants.
* http://chipkit.net/ can run the same sketches but with a MIPS/PIC underneath
See also the Arduino-compatible [JeeNode](wiki:JeeNode).
2013/02/02: I'm starting to look at building an Arduino stack, because I will be able to get closer to the hardware and thus possibly do things better that require tight timing constraints, for example.
A candidate initial board is the [Arduino UNO ATMEGA328P R3 Mainboard][2] as sold by Maplin.
There may be problems further down the line such as operational voltage range and so on that make Arduino less good than say the PICAXE for battery-powered solutions, but it will be good to provide two easy-to-start-from reference stacks that don't require lots of expensive dedicated programmers, etc.
See Mike Stirling's probably-Arduino-friendly FHT8V/FS20 radio driver code for the [RFM22/RFM23][3].
See [this item on low-power (battery) operation][4] for the ATmega and also [how to run an Arduino for months on a 9V battery][5].
Bo is suggesting use of RS 429-307 USB-serial TTL cable,FT232RQ,TTL-232R to program (small) Arduino boards; some come with only an ftdi header...
ATMEGA328P-PU
-------------
DHD20130407: have ordered a couple of these for initial breadboarding fun, plus 4MHz and 16MHz crystals (though apparently can run internal 8MHz RC oscillator internally divided by two for 1.8V, and accurate enough for serial up to about 57k6).
See directions to [get bootloader into ATmega][10] and use a [standalone breadboarded ATmega][11].
See [this SparkFun tutorial][12] on low-power running using the internal 8MHz RC clock plus external 32768Hz xtal.
And also https://www.sparkfun.com/products/10870
See also this [fuse settings calculator][13]. For an ATmega328P with 8MHz internal RC clock, internal /8, and BOD set at 1.8V, the calculator suggests avrdude args of -U lfuse:w:0x62:m -U hfuse:w:0xd9:m -U efuse:w:0xfe:m
Dynamic adjustment of clock speeds: http://www.nongnu.org/avr-libc/user-manual/group__avr__power.html
Sleep and BOD: http://www.nongnu.org/avr-libc/user-manual/group__avr__sleep.html
Note board detector stuff in here; http://www.gammon.com.au/forum/?id=11637
Note: add below to existing IDE boards.txt rather than in separate file as suggested in tutorials as else with IDE 1.0.3 at least you will have compilation problems (all the headers will be looked for alongside your new boards.txt):
##############################################################
atmega328bb2AA.name=ATmega328 on a breadboard (8 MHz internal clock / 8), 2xAA supply 1.8V BOD
atmega328bb2AA.upload.protocol=stk500
atmega328bb2AA.upload.maximum_size=30720
atmega328bb2AA.upload.speed=57600
atmega328bb2AA.bootloader.low_fuses=0x62
atmega328bb2AA.bootloader.high_fuses=0xDA
atmega328bb2AA.bootloader.extended_fuses=0x06
atmega328bb2AA.bootloader.path=arduino:atmega
atmega328bb2AA.bootloader.file=ATmegaBOOT_168_atmega328_pro_8MHz.hex
atmega328bb2AA.bootloader.unlock_bits=0x3F
atmega328bb2AA.bootloader.lock_bits=0x0F
atmega328bb2AA.build.mcu=atmega328p
atmega328bb2AA.build.f_cpu=1000000L
atmega328bb2AA.build.core=arduino
atmega328bb2AA.build.variant=standard
See note on programming breadboarded ATmega via FTDI USB cable: http://arduino.cc/forum/index.php?topic=118925.0
Using the TTL-232R-3V3 cable for breadboarded ATmega. Note that Vcc *from* the cable will be at 5V and may need to be down regulated. CTS should be connected to 0V. https://github.com/jimjibone/thermostat/tree/master/hardware
More low-power stuff: http://www.gammon.com.au/forum/?id=11497
See a working Optiboot 5 loader for 1MHz CPU and 4800 baud upload and 1.8V BOD here: http://www.earth.org.uk/OpenTRV/Arduino/bootloader/ATmega328P-1MHz/index.html
Some nice USART/serial low-level stuff here: http://www.appelsiini.net/2011/simple-usart-with-avr-libc
See the useful Disassembler plug-in for the 1.0.3 IDE, which allows viewing the assembler and thus to tune the source code significantly to optimise it while remaining nominally portable: http://www.forward.com.au/pfod/ArduinoProgramming/index.html
Ubuntu, Arduino IDE and installing the latest release of the controller software
--------------------------------------------------------------------------------
1 Install the arduino IDE
apt-get install arduino arduino-core
2 Edit your Arduino IDE installation's boards.txt file using your favourite text editor
sudo vi /usr/share/arduino/hardware/arduino/boards.txt
Append this text:
##############################################################
atmega328_1mhz_4800baud.name=Optiboot 5 for bare 328P, 1MHz internal clock, fast start, 1.8V BOD, 4800baud upload, c/o tim7 20130416
atmega328_1mhz_4800baud.upload.protocol=arduino
atmega328_1mhz_4800baud.upload.maximum_size=32256
atmega328_1mhz_4800baud.upload.speed=4800
atmega328_1mhz_4800baud.bootloader.low_fuses=0x42
atmega328_1mhz_4800baud.bootloader.high_fuses=0xde
atmega328_1mhz_4800baud.bootloader.extended_fuses=0x06
atmega328_1mhz_4800baud.bootloader.path=atmega
atmega328_1mhz_4800baud.bootloader.file=atmega328_1b.hex
atmega328_1mhz_4800baud.bootloader.unlock_bits=0x3F
atmega328_1mhz_4800baud.bootloader.lock_bits=0x2F
atmega328_1mhz_4800baud.build.mcu=atmega328p
atmega328_1mhz_4800baud.build.f_cpu=1000000L
atmega328_1mhz_4800baud.build.core=arduino
atmega328_1mhz_4800baud.build.variant=standard
3 Download this bootloader [atmega328_1b.hex](http://www.earth.org.uk/OpenTRV/Arduino/bootloader/ATmega328P-1MHz/atmega328_1b.hex)
Here is its content:
:107E0000112484B714BE81FFE6D085E08093810001
:107E100082E08093C00088E18093C10086E0809377
:107E2000C20089E18093C4008EE0BFD0259A86E02D
:107E300023EC3FEF91E0309385002093840096BBC4
:107E4000B09BFECF1D9AA8958150A9F7EE24FF2480
:107E5000AA24A394B5E0CB2EA1E1BA2EF3E0DF2E45
:107E600098D0813461F495D0082FA5D0023829F13B
:107E7000013811F485E001C083E083D07FC08234F3
:107E800011F484E103C0853419F485E09CD076C0F8
:107E9000853579F47ED0E82EFF247BD0082F10E0C2
:107EA000102F00270E291F29000F111F84D07801E1
:107EB00065C0863521F484E086D080E0DECF84364C
:107EC00009F040C066D065D0082F63D080E0E81686
:107ED00080E7F80618F4F701D7BEE895C0E0D1E0D6
:107EE00058D089930C17E1F7F0E0EF16F0E7FF06A2
:107EF00018F0F701D7BEE8955ED007B600FCFDCFBD
:107F0000A701A0E0B1E02C9130E011968C9111977F
:107F100090E0982F8827822B932B1296FA010C0160
:107F2000A7BEE89511244E5F5F4FF1E0A038BF0770
:107F300051F7F701C7BEE89507B600FCFDCFB7BE05
:107F4000E8951CC0843761F424D023D0082F21D0B9
:107F500032D0F70185917F0114D00150D1F70EC0C6
:107F6000853739F428D08EE10CD085E90AD08FE02E
:107F700084CF813511F488E018D01DD080E101D084
:107F80006FCF982F8091C00085FFFCCF9093C600E3
:107F900008958091C00087FFFCCF8091C00084FDD0
:107FA00001C0A8958091C6000895E0E6F0E098E150
:107FB000908380830895EDDF803219F088E0F5DF4B
:107FC000FFCF84E1DECF1F93182FE3DF1150E9F7D5
:107FD000F2DF1F910895282E80E0E7DFEE27FF27CC
:027FE000099402
:027FFE0000057C
:0400000300007E007B
:00000001FF
Copy the file to /usr/share/arduino/hardware/arduino/bootloaders/atmega/
sudo cp atmega328_1b.hex /usr/share/arduino/hardware/arduino/bootloaders/atmega/atmega328_1b.hex
4 Fire up the Arduino IDE and plug in your board. Select
Tools ---> Board ---> Optiboot 5 for bare 328P, 1MHz internal clock, fast start, 1.8V BOD, 4800baud upload, c/o tim7 20130416
5 Plug in the FTDI cable to your OpenTRV controller. Note which pin is 5V and which is GND
6 Select Tools ---> Serial Port ---> /dev/ttyUSB0
7 Start the Arduino serial monitor (Tools ---> Serial Monitor) or shift-ctrl-M
8 (For command-line access run this terminal command to configure the USB serial port
stty -F /dev/ttyUSB0 cs8 4800 ignbrk -brkint -icrnl -imaxbel -opost -onlcr -isig -icanon -iexten -echo -echoe -echok -echoctl -echoke noflsh -ixon -crtscts
but not needed just for IDE use.)
9 Select 4800 baud in the bottom right hand corner menu of the Serial Monitor and newline only for line endings.
10 You should see a screen similar to this:
<< TODO: paste screenshot here >>
Any time you plug in the FTDI cable or close and reopen the Arduino IDE serial monitor window, the board will reset and display the POST sequence again
11 Congratulations! You can now talk to your OpenTRV controller. Here's a useful page on the [[TODO: debugging interface | <>]]
12 Download and extract the source of the controller firmware from
svn export svn.code.sf.net/p/opentrv/code-0/trunk/Arduino/V0p2_Main
If you don't already have subversion installed, it is in the repositories
sudo apt-get install subversion
13 Open the project in the Arduino IDE
File ---> Open ---> whereever you placed the subversion export/V0p2_Main.ino
14 From the V0p2_Main.ino window, upload the code onto your attached OpenTRV controller
File --> Upload (or ctrl+U)
15 It will take a few minutes and then you are good to go. Unplug the FTDI cable, power it up in place and control your TRVs. On! Off! On! Off!
Linux/OSX/*nix Command-line Connection to OpenTRV
--------------------------------------------------
You may be able to just plug in an FTDI cable and do something as simple as:
screen /dev/serial/by-id/usb-FTDI_TTL232R-3V3_FTGW5R3C-if00-port0 4800
or:
screen /dev/tty.usbserial-FTGACM4G 4800
(your exact ID/path will differ)
to set 4800 baud, 8 bits, no parity, one stop bit.
Though if you want to send multi-character commands interactively you may be better preparing them elsewhere and cut-n-pasting them in, including trailing line-end, in one go.
You should also set line endings (eg in the IDE serial monitor) to newline (NL or \n) only.
Updates
-------
MH20140102: Step by step instructions for Ubuntu (this is a first step at making suitable instructions for a motivated hacker without previous experience of the project)
BG20130317: Arduino client for MQTT: http://knolleary.net/arduino-client-for-mqtt/ (and on other side install server on @Raspberry_Pi sudo apt-get install mosquitto)!
DHD20130319: Arduino cloner might be way to minimise in-circuit programming costs.
DHD20130319: Arduino IR: see Ken Shirriff's [library][6]. Remember to use PWM pins (eg pins 2 & 3).
DHD20130319: Can do [FFT in real time][7], eg to detect human voices for occupancy detection. See also http://arduino.cc/forum/index.php/topic,38153.0.html
DHD20130319: http://openhomeautomation.net/monitor-your-home-remotely-using-the-arduino-wifi-shield/
DHD20130404: [Arduino/Java serial][8] and non-RXTX Java serial: http://www.sparetimelabs.com/purejavacomm/purejavacomm.php
DHD20130407: Given this [minimal ATmega circuit][9] I've ordered some ATmega328-PU (plastic DIL) plus crystals and caps to start breadboarding.
BG20130408: The guy from http://openhomeautomation.net/ has started a series of videos on how to design your own Arduino shield, taking a weather shield as an example: http://www.youtube.com/watch?v=_NjZxK3acMs. So maybe we could consider one of those as part of the Arduino stack? And even if we end up with more compact designs, thinking of it from the point of view of designing a shield would help gel what pins to use for what. In particular, one of the good points he has in the video is that you'd want a custom shield to be compatible with other standard shields such as the Ethernet or the Wi-Fi one so that they can work together. (DHD response: Yes, I think that that's a decent goal, so that of nothing else people can run all or part of their systems actually on an unmodified Arduino for example. The biggest issue there is likely to be level shifting for (for example) the RFM23 that would not enjoy 5V. I'm looking at an [outline standard pin use][15].)
See this [example of measuring Vcc (battery?) voltage][17] with maybe +/-10% error but no external parts...
[2]: http://www.maplin.co.uk/arduino-uno-atmega328p-r3-mainboard-612955
[3]: http://mikestirling.co.uk/2013/02/implementing-the-elv-fht-protocol-with-an-rfm23/
[4]: http://www.surprisingedge.com/low-power-atmegatiny-with-watchdog-timer/
[5]: http://hwstartup.wordpress.com/2013/03/11/how-to-run-an-arduino-on-a-9v-battery-for-weeks-or-months/
[6]: http://www.righto.com/2009/08/multi-protocol-infrared-remote-library.html
[7]: https://code.google.com/p/neuroelec/source/browse/trunk/libraries/Arduino%201.0/ffft
[8]: http://playground.arduino.cc/Interfacing/Java
[9]: http://www.gammon.com.au/forum/?id=11109
[10]: http://arduino.cc/en/Tutorial/ArduinoISP
[11]: http://arduino.cc/en/Main/Standalone
[12]: https://www.sparkfun.com/tutorials/309
[13]: http://www.engbedded.com/fusecalc/
[14]: http://birdslikewires.co.uk/articles/saving-power-with-your-atmega
[15]: http://www.practicalmaker.com/blog/arduino-shield-design-standards
[16]: http://www.forward.com.au/pfod/ArduinoProgramming/index.html
[17]: https://github.com/jcw/jeelib/blob/master/examples/Ports/bandgap/bandgap.ino