This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
The components were chosen so that the hole project would be as low cost as possible. This is why radio has been retained as a means of communication, and also explains why the ATtiny was so appropriate too. Using standard project enclosures allow to drive the costs down as well.
- 2x Arduino Uno (possible to work with only one, doesn't need to be a Uno)
The first thing we need to do is to test our components individually in order to make sure that everything is working properly.
###Initial set-up
Since you are following this tutorial you are probably already used to uploading code to an Arduino. But this time it is a little different since we need to flash the ATtiny chip itself. You obviously can't use a usb cable directly like we do with the Uno.
Follow [this guide]() in order to **flash your device** and test it with the famous blink sketch.
Now that you followed the guide, you should know about cores packages. This is where things get messy. In order to use one of the libraries that we require, we need to install a specific core package on a (very) specific version of Arduino. And to add to the complexity, the ISP sketch that we upload to the Arduino the serves as a bridge between the computer and the ATtiny will need to come from yet another version of Arduino.
So let's get to it. You shouldn't be lost if you follow the following steps:
1. - [Download the Arduino IDE](https://www.arduino.cc/en/Main/OldSoftwareReleases) version **1.0.0** and **1.6.0**. Be careful here, not the 1.0.1, and not the 1.6.2. **1.0.0** and **1.6.0**. Less hair pulling will ensue if you make no mistake here.
2. - [Download the core package](https://code.google.com/p/arduino-tiny/) for Arduino 1.0 and set-up following the procedure explained in the [guide]().
3. [Download](https://github.com/mchr3k/arduino-libs-manchester) and install the [Manchester library](http://mchr3k.github.io/arduino-libs-manchester/) for the Arduino IDE **1.0.0**. [This page](https://www.arduino.cc/en/Guide/Libraries#toc5) from the Arduino website will help you with that if need be.
4. [Download] and install the Neopixel library for the Arduino IDE **1.0.0**.
5. Upload the **ArduinoISP** example sketch from the Arduino IDE **1.6.0** to the Arduino used as **bridge**.
6. Connect the Arduino to the ATtiny following the fritzing diagram in the guide. In **Tools**->**Board** select **ATtiny85 @ 8 MHz (internal oscillator; BOD disabled)**.
7. Upload you sketch to the ATtiny from the Arduino **1.0.0** IDE.
Not lost yet ? Let's move on.
###RF emitter/receptor + Arduino
Using an Arduino will help us debug the circuit more easily if something goes wrong. Only when we made sure that our components are not faulty will we start using the ATtinys.
####Basic communication
Sending data over bluetooth is not very different from using a Serial connection. You just send something and in and it comes out on the other side unaltered. With RF, things are quite different. It is very difficult to get unscrambled information on the receiving end, so forget about simply using **println()**.
Hopefully great minds worked at solved this kind of problem before us and came up with very efficient solutions. One of them is the [Manchester encoding](https://en.wikipedia.org/wiki/Manchester_code). The chief advantage of Manchester encoding is the fact that the signal synchronizes itself. This minimizes the error rate and optimizes reliability.
We will test the modules using two Arduinos. You might not have access to two different microcontrollers in which case you can use the ATtiny directly to test your set-up (but don't forget to use the right pin numbers).
The circuit is simple: we simply provide power to both modules and use a 330μF decoupling capacitor on the receiver side. This capacitor filters parasitic signals on the power line and greatly improves the reception.
[Upload this code]() to the **emitter** device and [that one]() to the receptor. The emitter simply increases a **counter** each time its main loop is executed. The value of the counter is sent using the Manchester library to the receptor which prints it to the **Serial console**. Both devices use their built-in LED as a communication status light.
The IR sensor is composed of two parts: an IR LED and a phototransistor reactic to a specific wavelength. The LED is wired as any other: in serie with a current limitting resistor.
We use a pull-up design to connect the sensor. Remember that the sensor is simply a phototransistor: the IR light it is subjected to creates a small current to the gate, provoking a larger current to flow between the collector and the drain. It means that when no light is applied to the sensor, it basically acts as a resistor of near infinite resistance.
Now what happens to the value measured on the analog pin? When no light is applied to the sensor, its resistance is high and the current flows to the analog pin: it reads a high voltage (5v or 1023 in analog terms) - it is *pulled up*.
When the sensor starts receiving IR light, its resistance decreases and current start flowing through it: less current flows through the analog pin and the value measured decreases.
[Upload the following code]() to the emitter microcontroller. It is a variation of the last one: instead of sending the value of the counter, we send the readings from the analog pin.
####Adding an antenna
You might have seen that we left out a pin on each RF module: the *Antenna* pin. You can increase the range of the system by hooking up a piece of wire to it.
How can we choose the length of the wire ? By following the formula of course!
Let *L* be the length of the wire, *f* the frequency of the radio signal we are transmitting and *c* lightspeed.
λ = c/f
λ = 299'792'458/315'000'000
λ = 0.9516
*L* = λ/4
*L* = 0.2378m = 23.78cm
The wire used as antenna should be approximatly 24cm long. Make your tests in order to see what works out best for you: type of wire, coiled or not etc.
###Switching to the ATtiny85
Now that we have the basics laid-out we can start working with our tiny microcontrollers.
####ATtiny + Neopixel
We will begin by ensuring that the Neopixel's library works well with the ATtiny. It is a good excuse to lay out the external power supply using the **power jack**, the **voltage regulator** and the **wall wart**.
We start by testing the emitter so that we can still use an Arduino on the receiver side and print the incoming messages for debugging purposes.
You can use the same code as [before]() as it includes pin numbers for the ATtiny. As before, you should see the value read from the sensor on the Serial console of the receiver.
When you are done with your tests, [upload the final version]() of the code for the emitter. What it does it implement a sleeping mechanism so that we can save as much power as possible on the device since it runs on batteries. We don't use the status LED anymore for the same reasons. When the device wakes up, it takes a reading from the IR sensor and sends it 20 times. We do that because depending on the conditions the RF link can be very weak so we want to ensure that the message is received before going back to sleep.
Time to go full ATtiny! Nothing complicated here: the ATtiny is substituted to the Arduino on the receiver side and we add the Neopixel that we tested earlier.
A sign that we're getting close the end, you can [upload the definitive code]() to the receptor. It uses the IR sensor's value received to light up the status LED in either **green** or **red**.
If it works, congratulations the hardest part is behind you!