How To Make Wireless Camera | Hasan Hacks And Technology
Step 1: Some Initial Thoughts...
So, how are we going to build this thing?
Microcontroller
The heart and soul of this project is an AVR ATMega8. It is essentially a slightly trimmed version of the ATMega168 chip that Arduino use. It is programmable in C or Assembly and has a variety of really useful features that we can use to our advantage.
" 28 pins, the majority of which are input/output (i/o)
" Onboard analog to digital converter
" Low power consumption
" 3 onboard timers
" Internal or external clock source
" Lots of code libraries and samples online
Having lots of pins is good. We can interface with an LCD screen, have 6 button inputs and still have enough left over for an IR LED to shoot with and some status LEDs.
The Atmel AVR series of processors has a lot of support online and there are plenty of tutorials in getting started (i will go over this briefly, but there are better dedicated tutorials) and heaps and heaps of code to mull over. For reference i ll be coding this project in C using the AVR-LibC library.
I could have easily gone with PIC to do this, but AVR is well supported and all the examples i've found for remotes have been AVR based!
LCD Display
There are two main types of display, graphical and alphanumeric. Graphical displays have a resolution and you can put pixels wherever you like. The downside is that they re harder to code for (although libraries exist). Alphanumeric displays are simply one or more rows of characters, the LCD has an onboard store of basic characters (i.e. the alphabet, some numbers and symbols) and it s relatively easy to output strings and so on. The downside is they re not as flexible and displaying graphics is virtually impossible, but it suits our purpose. They re also cheaper!
Alphanumerics are categorised by their row and column count. The 2x16 is pretty common, with two rows of 16 characters, each character being a 5x8 matrix. You can get 2x20 s too, but i don t see the need. Buy whatever you feel comfortable with.
I chose to use a red backlit LCD (i want to use this for astrophotography and red light is better for night vision). You can go without a backlight - it s entirely your choice. If you choose a non backlit route you will save power and money, but you might need a torch in the dark.
When looking for an LCD, you should ensure that it is controlled by the HD44780. It is an industry standard protocol developed by Hitachi and there are a lot of good libraries that we can use to output data. The model i bought was a JHD162A from eBay.
Input
Input will be done by buttons (simple!). I chose 6 - mode select, ok/shoot and 4 directions. It s also worth getting another small button for resetting the micro in case of a crash.
As for the trigger input, some basic ideas are a light dependent resistor or an electret microphone. This is where you can get creative or stingy depending on your budget. Ultrasound sensors will cost a bit more and require some extra programming but you can do some really neat stuff with them.
Most people will be happy with a microphone (probably the most useful general sensor) and electrets are very cheap. Be aware that it ll need to be amplified too (but i ll go over this later).
Output - Status
The only real output we need is status (besides the display), so a couple of LEDs will work fine here.
Output - Shooting
For taking pictures, we need to interface with the camera and for that we need a light source that can produce infra-red radiation. Thankfully there are a multitude of LEDs that do this and you should try to pick up a reasonably high power one. The unit i chose has a current rating of 100mA max (most LEDs are around 30mA).
You should also take care to note the wavelength output. Infrared light is in the longer wavelength part of the EM spectrum and you should be looking for a value of around 850-950nm. Most IR LEDs tend towards the 950 end and you may see a bit of red light when it's turned on, this isn't a problem, but it's wasted spectrum so try to go closer to 850 if possible.
Power
How are we going to power all this? Well, it's going to be portable so batteries! I chose to use 2 AA batteries which are then stepped up to 5V. I'll go over the reasoning behind this in the next few sections.
'Casing and Construction'
How you do this bit is entirely up to you. I decided to use stripboard for the circuit after prototyping because it's cheap and flexible and saves designing a custom PCB. I have provided the schematics so you are free to make your own PCB layout - although if you do, i would be grateful to have a copy!
Again the case is entirely your choice, it needs to be able to fit the screen, buttons (in a fairly intuitive layout if possible) and the batteries. As circuit boards go, this one isn't that complicated, a lot of the connections are simply to things like the buttons/LCD.
Microcontroller
The heart and soul of this project is an AVR ATMega8. It is essentially a slightly trimmed version of the ATMega168 chip that Arduino use. It is programmable in C or Assembly and has a variety of really useful features that we can use to our advantage.
" 28 pins, the majority of which are input/output (i/o)
" Onboard analog to digital converter
" Low power consumption
" 3 onboard timers
" Internal or external clock source
" Lots of code libraries and samples online
Having lots of pins is good. We can interface with an LCD screen, have 6 button inputs and still have enough left over for an IR LED to shoot with and some status LEDs.
The Atmel AVR series of processors has a lot of support online and there are plenty of tutorials in getting started (i will go over this briefly, but there are better dedicated tutorials) and heaps and heaps of code to mull over. For reference i ll be coding this project in C using the AVR-LibC library.
I could have easily gone with PIC to do this, but AVR is well supported and all the examples i've found for remotes have been AVR based!
LCD Display
There are two main types of display, graphical and alphanumeric. Graphical displays have a resolution and you can put pixels wherever you like. The downside is that they re harder to code for (although libraries exist). Alphanumeric displays are simply one or more rows of characters, the LCD has an onboard store of basic characters (i.e. the alphabet, some numbers and symbols) and it s relatively easy to output strings and so on. The downside is they re not as flexible and displaying graphics is virtually impossible, but it suits our purpose. They re also cheaper!
Alphanumerics are categorised by their row and column count. The 2x16 is pretty common, with two rows of 16 characters, each character being a 5x8 matrix. You can get 2x20 s too, but i don t see the need. Buy whatever you feel comfortable with.
I chose to use a red backlit LCD (i want to use this for astrophotography and red light is better for night vision). You can go without a backlight - it s entirely your choice. If you choose a non backlit route you will save power and money, but you might need a torch in the dark.
When looking for an LCD, you should ensure that it is controlled by the HD44780. It is an industry standard protocol developed by Hitachi and there are a lot of good libraries that we can use to output data. The model i bought was a JHD162A from eBay.
Input
Input will be done by buttons (simple!). I chose 6 - mode select, ok/shoot and 4 directions. It s also worth getting another small button for resetting the micro in case of a crash.
As for the trigger input, some basic ideas are a light dependent resistor or an electret microphone. This is where you can get creative or stingy depending on your budget. Ultrasound sensors will cost a bit more and require some extra programming but you can do some really neat stuff with them.
Most people will be happy with a microphone (probably the most useful general sensor) and electrets are very cheap. Be aware that it ll need to be amplified too (but i ll go over this later).
Output - Status
The only real output we need is status (besides the display), so a couple of LEDs will work fine here.
Output - Shooting
For taking pictures, we need to interface with the camera and for that we need a light source that can produce infra-red radiation. Thankfully there are a multitude of LEDs that do this and you should try to pick up a reasonably high power one. The unit i chose has a current rating of 100mA max (most LEDs are around 30mA).
You should also take care to note the wavelength output. Infrared light is in the longer wavelength part of the EM spectrum and you should be looking for a value of around 850-950nm. Most IR LEDs tend towards the 950 end and you may see a bit of red light when it's turned on, this isn't a problem, but it's wasted spectrum so try to go closer to 850 if possible.
Power
How are we going to power all this? Well, it's going to be portable so batteries! I chose to use 2 AA batteries which are then stepped up to 5V. I'll go over the reasoning behind this in the next few sections.
'Casing and Construction'
How you do this bit is entirely up to you. I decided to use stripboard for the circuit after prototyping because it's cheap and flexible and saves designing a custom PCB. I have provided the schematics so you are free to make your own PCB layout - although if you do, i would be grateful to have a copy!
Again the case is entirely your choice, it needs to be able to fit the screen, buttons (in a fairly intuitive layout if possible) and the batteries. As circuit boards go, this one isn't that complicated, a lot of the connections are simply to things like the buttons/LCD.
Step 2: Power Management
Power Management
For a project like this it's obvious that portability should be a key aspect. Batteries are thus the logical choice!
Now, for portable devices it is fairly key that you pick a battery source that is either rechargeable or easily available. The two main options are the 9V PP3 battery or AA batteries. I'm sure some people will assume that a 9V battery is the best option because hey, 9V is better than 3 right?
Well, not in this case. 9V batteries whilst very useful, produce their voltage at the expense of battery life. Measured in mAh (milliamp hours), this rating tells you in theory how long a battery will last operating at 1mA in hours (though take it with a pinch of salt, these are often under ideal, low load conditions). The higher the rating, the longer the battery will last. 9V batteries are rated at up to and around 1000mAh. Alkaline AA's on the other hand have almost three times as much at 2900mAh. NiMH rechargables can reach this, though 2500mAh is a reasonable amount (note that rechargeable batteries operate at 1.2V not 1.5!).
The LCD screen needs a 5V input (�10%) and the AVR (the microcontroller) needs roughly the same (though it can go as low as 2.7 for low frequency clock speeds). We also need a fairly stable voltage, if it fluctuates about it could cause problems with the microcontroller.
To do this we'll use a voltage regulator, you need to make a choice over price vs efficiency now. You've got the option of using a simple 3-pin voltage regulator like the LM7805 (78 series, +5 volts output) or a small integrated circuit.
Using a simple regulator
If you choose to go with this option, you need to bear a few points in mind. Firstly, three pin regulators almost always need an input that is higher than their output. They then step the voltage down to the desired value. The downside is that they have awful efficiency (50-60% is good going). The upside is that they're cheap and will run with a 9V battery, you can pick up a basic model for 20 pence in the UK. You should also bear in mind that regulators have a dropout voltage - the minimum gap between input and output. You can buy special LDO (Low DropOut) regulators that have dropouts at around 50mV (compared to 1-2V with other designs). In other words, look out for LDOs with a +5V output.
Using an integrated circuit
The ideal way to go is a switching regulator. These will be, for our purpose, normally 8-pin packages that take in a voltage and give us a regulated output at a high efficiency - almost 90% in some cases. You can get step up or step down converters (boost/buck respectively) depending on what you want to put in, alternatively you can buy regulators that will take either above or below the desired output.
The chip i'm using for this project is a MAX619+. It is a 5V step up regulator that takes 2 AA's (the input range is 2V-3.3V) and gives a steady 5V out. It only needs four capacitors to operate and is very space efficient. Cost - �3.00 including the caps. Arguably it's worth the splurge just to get a bit more use out of your batteries. The only major downside is that it's not short circuit protected, so if there is a current surge, be warned! This is reasonably trivial to fix with an add on circuit however:
Another useful chip design - although not nearly as neat a solution is the LT1307. Again, a 5V regulator, but it can take a variety of inputs and has useful things like low battery detection. It costs quite a bit more at nearly �5 with inductors, large capacitors and resistors.
Voltage Rails
We are going to be using two main voltage rails (plus a common ground). The first will be the 3V from the battery, this will be used to power the LEDs and other relatively high power components. My MAX619 is only rated up to 60mA (though absolute maximum is 120mA) so it is easier to connect the microcontroller to a MOSFET to control any LEDs. The MOSFET draws almost no current and acts as a break in the circuit when the gate input is under around 3V. When the microcontroller sends out logical 1 on the pin, the voltage is 5V and the FET turns on, then just acting as a short circuit (i.e. a piece of wire).
The 5V rail will power the LCD, Microcontroller and any amplification circuits for input sensors.
Power Consumption
If we look at various datasheets, we note that the AVR takes no more than 15-20mA at maximum load.
The LCD takes only 1mA to operate (at least when i tested, budget for 2). With the backlight on, it's really up to you to decide. Connecting it straight up to the 5V rail (i tried) is fine, but make sure it has an onboard resistor (follow the traces on the PCB) before you do. It drew 30mA that way - terrible! With a 3.3k resistor it's still viewable (perfect for astro photography) and only draws 1mA. You can still get decent brightness using a 1k or otherwise. I'm fine with mine drawing just under 2mA with the backlight on! If you want, it's trivial to add a brightness knob using a 10k potentiometer.
The IR LED might take 100mA maximum, but i have had good results with 60mA across mine (experiment!). You can then halve that current because you're effectively running at a 50% duty cycle (when the LED is modulated). Anyway, it's only on for a fraction of a second so we don't need to worry about this.
The other LEDs you should play about with, you may find that only a 10mA current is enough to give you a good brightness - certainly look for low power LEDs (excluding the IR one), you re not designing a torch! I chose not to add a power indicator in my circuit, simply because it's a lot of current draw for not much use. Use the on/off switch to check if it's on!
In total, you shouldn't be running more than 30mA at any one time and with a theoretical supply of around 2500 (allowing for variation) mAh that should give you well over 80 hours straight with everything on. With the processor idled for most of the time this will at least double/triple, so you shouldn't have to change your batteries very often.
Conclusion
There we go, that was easy wasn't it! You can either go cheap and cheery with a 9V battery and a LDO regulator at the expense of efficiency or pay a bit more and use a dedicated IC to do it. My budget was still under �20 even WITH the IC, so you can drop it even further if you need to.
For a project like this it's obvious that portability should be a key aspect. Batteries are thus the logical choice!
Now, for portable devices it is fairly key that you pick a battery source that is either rechargeable or easily available. The two main options are the 9V PP3 battery or AA batteries. I'm sure some people will assume that a 9V battery is the best option because hey, 9V is better than 3 right?
Well, not in this case. 9V batteries whilst very useful, produce their voltage at the expense of battery life. Measured in mAh (milliamp hours), this rating tells you in theory how long a battery will last operating at 1mA in hours (though take it with a pinch of salt, these are often under ideal, low load conditions). The higher the rating, the longer the battery will last. 9V batteries are rated at up to and around 1000mAh. Alkaline AA's on the other hand have almost three times as much at 2900mAh. NiMH rechargables can reach this, though 2500mAh is a reasonable amount (note that rechargeable batteries operate at 1.2V not 1.5!).
The LCD screen needs a 5V input (�10%) and the AVR (the microcontroller) needs roughly the same (though it can go as low as 2.7 for low frequency clock speeds). We also need a fairly stable voltage, if it fluctuates about it could cause problems with the microcontroller.
To do this we'll use a voltage regulator, you need to make a choice over price vs efficiency now. You've got the option of using a simple 3-pin voltage regulator like the LM7805 (78 series, +5 volts output) or a small integrated circuit.
Using a simple regulator
If you choose to go with this option, you need to bear a few points in mind. Firstly, three pin regulators almost always need an input that is higher than their output. They then step the voltage down to the desired value. The downside is that they have awful efficiency (50-60% is good going). The upside is that they're cheap and will run with a 9V battery, you can pick up a basic model for 20 pence in the UK. You should also bear in mind that regulators have a dropout voltage - the minimum gap between input and output. You can buy special LDO (Low DropOut) regulators that have dropouts at around 50mV (compared to 1-2V with other designs). In other words, look out for LDOs with a +5V output.
Using an integrated circuit
The ideal way to go is a switching regulator. These will be, for our purpose, normally 8-pin packages that take in a voltage and give us a regulated output at a high efficiency - almost 90% in some cases. You can get step up or step down converters (boost/buck respectively) depending on what you want to put in, alternatively you can buy regulators that will take either above or below the desired output.
The chip i'm using for this project is a MAX619+. It is a 5V step up regulator that takes 2 AA's (the input range is 2V-3.3V) and gives a steady 5V out. It only needs four capacitors to operate and is very space efficient. Cost - �3.00 including the caps. Arguably it's worth the splurge just to get a bit more use out of your batteries. The only major downside is that it's not short circuit protected, so if there is a current surge, be warned! This is reasonably trivial to fix with an add on circuit however:
Another useful chip design - although not nearly as neat a solution is the LT1307. Again, a 5V regulator, but it can take a variety of inputs and has useful things like low battery detection. It costs quite a bit more at nearly �5 with inductors, large capacitors and resistors.
Voltage Rails
We are going to be using two main voltage rails (plus a common ground). The first will be the 3V from the battery, this will be used to power the LEDs and other relatively high power components. My MAX619 is only rated up to 60mA (though absolute maximum is 120mA) so it is easier to connect the microcontroller to a MOSFET to control any LEDs. The MOSFET draws almost no current and acts as a break in the circuit when the gate input is under around 3V. When the microcontroller sends out logical 1 on the pin, the voltage is 5V and the FET turns on, then just acting as a short circuit (i.e. a piece of wire).
The 5V rail will power the LCD, Microcontroller and any amplification circuits for input sensors.
Power Consumption
If we look at various datasheets, we note that the AVR takes no more than 15-20mA at maximum load.
The LCD takes only 1mA to operate (at least when i tested, budget for 2). With the backlight on, it's really up to you to decide. Connecting it straight up to the 5V rail (i tried) is fine, but make sure it has an onboard resistor (follow the traces on the PCB) before you do. It drew 30mA that way - terrible! With a 3.3k resistor it's still viewable (perfect for astro photography) and only draws 1mA. You can still get decent brightness using a 1k or otherwise. I'm fine with mine drawing just under 2mA with the backlight on! If you want, it's trivial to add a brightness knob using a 10k potentiometer.
The IR LED might take 100mA maximum, but i have had good results with 60mA across mine (experiment!). You can then halve that current because you're effectively running at a 50% duty cycle (when the LED is modulated). Anyway, it's only on for a fraction of a second so we don't need to worry about this.
The other LEDs you should play about with, you may find that only a 10mA current is enough to give you a good brightness - certainly look for low power LEDs (excluding the IR one), you re not designing a torch! I chose not to add a power indicator in my circuit, simply because it's a lot of current draw for not much use. Use the on/off switch to check if it's on!
In total, you shouldn't be running more than 30mA at any one time and with a theoretical supply of around 2500 (allowing for variation) mAh that should give you well over 80 hours straight with everything on. With the processor idled for most of the time this will at least double/triple, so you shouldn't have to change your batteries very often.
Conclusion
There we go, that was easy wasn't it! You can either go cheap and cheery with a 9V battery and a LDO regulator at the expense of efficiency or pay a bit more and use a dedicated IC to do it. My budget was still under �20 even WITH the IC, so you can drop it even further if you need to.
Step 3: A Closer Look at the ATmega8
Pins
Image 1 is the pinout diagram for the ATMega8 (exactly the same as the 168/48/88, the only difference is the amount of onboard memory and interrupt options).
Pin 1 - Reset, should be held at VCC voltage (or at least logical 1). If grounded, the device will soft-reset
Pin 2-6 - Port D, general input/output
Pin 7 - VCC, supply voltage (+5V for us)
Pin 8 - Ground
Pin 9,10 - XTAL, external clock inputs (part of Port B)
Pin 11 - 13 Port D, general input/output
Pin 14 - 19 Port B, general input/output
Pin 20 - AVCC, analogue supply voltage (same as VCC)
Pin 21 - AREF, analogue voltage reference
Pin 22 - Ground
Pin 23-28 Port C, general input/output
Usable i/o ports: D = 8, C = 6, B = 6
A total of 20 usable ports is great, for simplicity you should group your outputs either into ports (say, D as the output port) or into groups on the board - you might want the LCD to run from Port C just to keep the wires tidy in that corner.
There are three extra pins that are required for programming. Those are MISO (18), MOSI (17) and SCK(19). These will happily act as i/o pins if needed though.
Clocking
The signal that we send to the camera needs to be precisely timed (accurate to around a microsecond) so it's important we choose a good clock source. All AVRs have an internal oscillator that the chip can get its clock from. The downside of this is that they can fluctuate around 10% with temperature/pressure/humidity. What we can do to combat this is use an external quartz crystal. These are available in anything from 32768kHz (watch) to 20MHz. I've chosen to use a 4Mhz crystal as it provides a decent amount of speed yet is fairly power conservative compared to perhaps 8Mhz+.
Onboard Power Management
I really wanted to use sleep routines in my code. In fact i wrote the first version to rely heavily on idling the processor while time lapsing. Unfortnuately, due to time constraints, i ran into some issues with running the clock externally and interrupting using the timers. In essence i'd have to rewrite the code to deal with the controller simply not waking up - which i could do, but time is against me. As such, the device only draws 20mA ish so you can get away with it. If you're really up for it, then by all means fiddle with the code, all you need to do is to clock internally and then run Timer 2 in asynchronous mode using the 4MHz crystal for the more accurate delays. It's simple to do, but time consuming.
ADC
The swiss army knife in the AVR toolset, the ADC stands for Analogue to Digital Converter. How it works is relatively simple from the outside. A voltage is sampled on a pin (from some sensor or other input), the voltage gets converted into a digital value between 0 and 1024. A value of 1024 will be observed when the input voltage is equal to the ADC reference voltage. If we set our reference to be VCC (+5V) then each division is 5/1024 V or around 5mV. Thus an increase of 5mV on the pin will increase the ADC value by 1. We can take the ADC output value as a variable and then fiddle with it, compare it with things, etc in the code. The ADC is an incredibly useful function and allows you do lots of cool things like turn your AVR into an oscilloscope. The sampling frequency is around 125kHz and must be set in proportion to the main clock frequency.
Registers
You may have heard of registers before, but fear not! A register is simply a collection of addresses (locations) in the AVR memory. Registers are classed by their bit size. A 7 bit register has 8 locations, as we start from 0. There are registers for just about everything and we ll take a look at them in much more detail later. Some examples include the PORTx registers (where x is B, C or D) that control whether a pin is set high or low and sets pull up resistors for inputs, the DDRx registers which set whether a pin is output or input and so on.
The Datasheet
A behemoth of literature, weighing in at around 400 pages; the AVR datasheets are an invaluable reference to your processor. They contain details of every register, every pin, how timers work, what fuses should be set to what and much more. They are free of charge and you will need it sooner or later, so download a copy!
www.atmel.com/dyn/resources/prod_documents/doc2486.pdf
Image 1 is the pinout diagram for the ATMega8 (exactly the same as the 168/48/88, the only difference is the amount of onboard memory and interrupt options).
Pin 1 - Reset, should be held at VCC voltage (or at least logical 1). If grounded, the device will soft-reset
Pin 2-6 - Port D, general input/output
Pin 7 - VCC, supply voltage (+5V for us)
Pin 8 - Ground
Pin 9,10 - XTAL, external clock inputs (part of Port B)
Pin 11 - 13 Port D, general input/output
Pin 14 - 19 Port B, general input/output
Pin 20 - AVCC, analogue supply voltage (same as VCC)
Pin 21 - AREF, analogue voltage reference
Pin 22 - Ground
Pin 23-28 Port C, general input/output
Usable i/o ports: D = 8, C = 6, B = 6
A total of 20 usable ports is great, for simplicity you should group your outputs either into ports (say, D as the output port) or into groups on the board - you might want the LCD to run from Port C just to keep the wires tidy in that corner.
There are three extra pins that are required for programming. Those are MISO (18), MOSI (17) and SCK(19). These will happily act as i/o pins if needed though.
Clocking
The signal that we send to the camera needs to be precisely timed (accurate to around a microsecond) so it's important we choose a good clock source. All AVRs have an internal oscillator that the chip can get its clock from. The downside of this is that they can fluctuate around 10% with temperature/pressure/humidity. What we can do to combat this is use an external quartz crystal. These are available in anything from 32768kHz (watch) to 20MHz. I've chosen to use a 4Mhz crystal as it provides a decent amount of speed yet is fairly power conservative compared to perhaps 8Mhz+.
Onboard Power Management
I really wanted to use sleep routines in my code. In fact i wrote the first version to rely heavily on idling the processor while time lapsing. Unfortnuately, due to time constraints, i ran into some issues with running the clock externally and interrupting using the timers. In essence i'd have to rewrite the code to deal with the controller simply not waking up - which i could do, but time is against me. As such, the device only draws 20mA ish so you can get away with it. If you're really up for it, then by all means fiddle with the code, all you need to do is to clock internally and then run Timer 2 in asynchronous mode using the 4MHz crystal for the more accurate delays. It's simple to do, but time consuming.
ADC
The swiss army knife in the AVR toolset, the ADC stands for Analogue to Digital Converter. How it works is relatively simple from the outside. A voltage is sampled on a pin (from some sensor or other input), the voltage gets converted into a digital value between 0 and 1024. A value of 1024 will be observed when the input voltage is equal to the ADC reference voltage. If we set our reference to be VCC (+5V) then each division is 5/1024 V or around 5mV. Thus an increase of 5mV on the pin will increase the ADC value by 1. We can take the ADC output value as a variable and then fiddle with it, compare it with things, etc in the code. The ADC is an incredibly useful function and allows you do lots of cool things like turn your AVR into an oscilloscope. The sampling frequency is around 125kHz and must be set in proportion to the main clock frequency.
Registers
You may have heard of registers before, but fear not! A register is simply a collection of addresses (locations) in the AVR memory. Registers are classed by their bit size. A 7 bit register has 8 locations, as we start from 0. There are registers for just about everything and we ll take a look at them in much more detail later. Some examples include the PORTx registers (where x is B, C or D) that control whether a pin is set high or low and sets pull up resistors for inputs, the DDRx registers which set whether a pin is output or input and so on.
The Datasheet
A behemoth of literature, weighing in at around 400 pages; the AVR datasheets are an invaluable reference to your processor. They contain details of every register, every pin, how timers work, what fuses should be set to what and much more. They are free of charge and you will need it sooner or later, so download a copy!
www.atmel.com/dyn/resources/prod_documents/doc2486.pdf
Step 4: Allocating Pins
I already mentioned the inputs and outputs we need, so we should allocate them pins!
Now, PORT D has 8 pins which is convenient as it can act as our output port. The LCD requires 7 pins to operate - 4 data pins and 3 control pins. The IR LED only requires one pin, so that makes up our 8.
PORTB is going to be our button port, it has 6 inputs, but we're only going to need 5. These will be the mode and directional buttons.
PORTC is special, it is the ADC port. We only need one pin for the trigger input and it makes sense to put it on PC0 (a common abbreviation for port pins in this case Port C, Pin 0). We then have a couple of pins for status LEDs (one lights up when the ADC value is above some condition, the other lights up when it is below some condition). We are also going to put our ok/shoot button input here, for reasons that will become clear later.
After all this, we've used up the majority of the ports but we still have a few left if you wish to expand the project - perhaps multiple triggers?
Now, PORT D has 8 pins which is convenient as it can act as our output port. The LCD requires 7 pins to operate - 4 data pins and 3 control pins. The IR LED only requires one pin, so that makes up our 8.
PORTB is going to be our button port, it has 6 inputs, but we're only going to need 5. These will be the mode and directional buttons.
PORTC is special, it is the ADC port. We only need one pin for the trigger input and it makes sense to put it on PC0 (a common abbreviation for port pins in this case Port C, Pin 0). We then have a couple of pins for status LEDs (one lights up when the ADC value is above some condition, the other lights up when it is below some condition). We are also going to put our ok/shoot button input here, for reasons that will become clear later.
After all this, we've used up the majority of the ports but we still have a few left if you wish to expand the project - perhaps multiple triggers?
Step 5: Communicating With the Camera
Note: Image used without permission from http://www.bigmike.it/ircontrol/
We will be using the IR emitter to send a signal to the camera. The upshot of this is it's wireless and there is NO WAY you can damage your camera (unlike connecting things into the camera's ports). If you manage to damage your camera by pointing an IR emitter at it, then I sure as hell hope you don't take your camera outside!
Software
There are two states that we need to emulate, and this is true for all cameras (indeed more or less all IR communication). Communications are sent as ones and zeroes, highs and lows.
First is the on state, when we send a logical one, we must turn the emitter on and off rapidly. We modulate the signal. Different manufacturers have different requirements for this. Nikon is 38.4kHz, Canon is somewhat less. 38k is around what most cameras use. To get this modulation we work out the period (1/f) to know that for, say, Nikon we need an on/off cycle every 26 microseconds. The modulation is symmetrical so on takes 13uS and off takes 13uS.
The Nikon sequence is (kudos to BigMike.it):
On for 2000uS
Off for 27830uS
On for 390uS
Off for 1580uS
On for 410uS
Off for 3580uS
On for 400uS
The sequence is pulsed once, there is a 63ms delay, then it is pulsed again and the shot is taken. Notice that most of those numbers are divisible by 13.
For Canon it is (with 32kHz modulation - thanks to http://www.doc-diy.net/photo/rc-1_hacked/index.php):
16 cycles (30uS each)
Pause for 7.3ms
16 cycles (30uS each)
Much simpler.
Using an accurate delay library in the code, as most of the Nikon sequences are multiples of 13, we can just use for loops to go through them to get the relevant amount of on time (off time is just a normal delay). There are some guides that suggest you use assembly to get the timing precise, but as the doc-diy link shows, the timing can be pretty far off and still produce a valid trigger signal.
The delay library i use bases its delays from clock cycles (knowing the clock frequency, and the number of clock cycles it takes to execute a given function) so it's very precise, but limited to your crystal accuracy.
Other Manufacturers
If you have a different camera, simply modify the shoot(); function in the final C code. It should be fairly easy if you know the modulation and the pulse sequence.
Olympus: http://olyflyer.blogspot.com/2007/07/how-to-make-your-own-rm-1-compatible.html
Pentax: http://sourceforge.net/projects/prcf/ look at the source code for the sequence
Hardware
As we're going to be using a high current emitter, we can't simply draw that current through the microprocessor (see the datasheet for absolute maximum ratings). What we're going to do instead is draw it straight from the battery with an electronic switch, the MOSFET.
Unlike a transistor which involves fiddly calculations to get collector and emitter currents, gain and so on, MOSFETs are surprisingly easy to use.
The 2N7000 i recommend you buy is a basic low power 'FET. There are many variety of 'FET that can be used to switch many Amps using microcontrollers that would just frazzle if they tried to draw that kind of current. There are three parts to the 'FET, the Source, the Gate and the Drain.
The source lead is connected to ground and the gate is connected to the microcontroller pin (i just connect them straight up, but you could always put a resistor in just in case - check the threshold voltage). The components you want to turn on are connected to the power rail and to the drain lead. When the threshold voltage is reached on the gate - i.e. when the micro turns on the pin - the 'FET starts conducting and acts like a wire to complete the circuit between the power rail and ground. For the 2N7000 the threshold is a maximum of 3V, minimum of 0.8V.
We also note that the drain-source resistance (the 'FET acts like a resistor bridging the components to ground) is very small - in the order of 5R max. Whilst this is very small, it could mean a noticeable drop in current across the emitter - you can compensate by lowering the value of the resistor that's in series with the emitter. This resistor, using the very useful LED Calculator () with 3V in, a forward voltage (check the emmiter's datasheet) of 1.7V and a current of 80mA to be 18R. You could get away with a 12R, but i would stick with 18R to be on the safe side. Image 1 shows how the circuit is wired up
V+ in this instance is our 3V rail. As the gate is seeing 5V, the LED would currently be turned on.
If you want to look into how MOSFETs actually work and more detailed technical information:
http://www.irf.com/technical-info/appnotes/mosfet.pdf
We will be using the IR emitter to send a signal to the camera. The upshot of this is it's wireless and there is NO WAY you can damage your camera (unlike connecting things into the camera's ports). If you manage to damage your camera by pointing an IR emitter at it, then I sure as hell hope you don't take your camera outside!
Software
There are two states that we need to emulate, and this is true for all cameras (indeed more or less all IR communication). Communications are sent as ones and zeroes, highs and lows.
First is the on state, when we send a logical one, we must turn the emitter on and off rapidly. We modulate the signal. Different manufacturers have different requirements for this. Nikon is 38.4kHz, Canon is somewhat less. 38k is around what most cameras use. To get this modulation we work out the period (1/f) to know that for, say, Nikon we need an on/off cycle every 26 microseconds. The modulation is symmetrical so on takes 13uS and off takes 13uS.
The Nikon sequence is (kudos to BigMike.it):
On for 2000uS
Off for 27830uS
On for 390uS
Off for 1580uS
On for 410uS
Off for 3580uS
On for 400uS
The sequence is pulsed once, there is a 63ms delay, then it is pulsed again and the shot is taken. Notice that most of those numbers are divisible by 13.
For Canon it is (with 32kHz modulation - thanks to http://www.doc-diy.net/photo/rc-1_hacked/index.php):
16 cycles (30uS each)
Pause for 7.3ms
16 cycles (30uS each)
Much simpler.
Using an accurate delay library in the code, as most of the Nikon sequences are multiples of 13, we can just use for loops to go through them to get the relevant amount of on time (off time is just a normal delay). There are some guides that suggest you use assembly to get the timing precise, but as the doc-diy link shows, the timing can be pretty far off and still produce a valid trigger signal.
The delay library i use bases its delays from clock cycles (knowing the clock frequency, and the number of clock cycles it takes to execute a given function) so it's very precise, but limited to your crystal accuracy.
Other Manufacturers
If you have a different camera, simply modify the shoot(); function in the final C code. It should be fairly easy if you know the modulation and the pulse sequence.
Olympus: http://olyflyer.blogspot.com/2007/07/how-to-make-your-own-rm-1-compatible.html
Pentax: http://sourceforge.net/projects/prcf/ look at the source code for the sequence
Hardware
As we're going to be using a high current emitter, we can't simply draw that current through the microprocessor (see the datasheet for absolute maximum ratings). What we're going to do instead is draw it straight from the battery with an electronic switch, the MOSFET.
Unlike a transistor which involves fiddly calculations to get collector and emitter currents, gain and so on, MOSFETs are surprisingly easy to use.
The 2N7000 i recommend you buy is a basic low power 'FET. There are many variety of 'FET that can be used to switch many Amps using microcontrollers that would just frazzle if they tried to draw that kind of current. There are three parts to the 'FET, the Source, the Gate and the Drain.
The source lead is connected to ground and the gate is connected to the microcontroller pin (i just connect them straight up, but you could always put a resistor in just in case - check the threshold voltage). The components you want to turn on are connected to the power rail and to the drain lead. When the threshold voltage is reached on the gate - i.e. when the micro turns on the pin - the 'FET starts conducting and acts like a wire to complete the circuit between the power rail and ground. For the 2N7000 the threshold is a maximum of 3V, minimum of 0.8V.
We also note that the drain-source resistance (the 'FET acts like a resistor bridging the components to ground) is very small - in the order of 5R max. Whilst this is very small, it could mean a noticeable drop in current across the emitter - you can compensate by lowering the value of the resistor that's in series with the emitter. This resistor, using the very useful LED Calculator () with 3V in, a forward voltage (check the emmiter's datasheet) of 1.7V and a current of 80mA to be 18R. You could get away with a 12R, but i would stick with 18R to be on the safe side. Image 1 shows how the circuit is wired up
V+ in this instance is our 3V rail. As the gate is seeing 5V, the LED would currently be turned on.
If you want to look into how MOSFETs actually work and more detailed technical information:
http://www.irf.com/technical-info/appnotes/mosfet.pdf
Step 6: Component/Supply List
SCHEMATIC: http://img8.imageshack.us/img8/8904/cameracontrollerschemat.png - read the breadboard tutorials first in case i've made any mistakes!
CPU/LCD
1x ATMega8 or ATMega88/168 (needs at least 8k of memory) £1.50
1x HD44780 compatible LCD screen £4-6 (6 if you want cool colours) from eBay
1x 10K Pot (or try different resistors) for contrast control 10-30p
1x 4MHz Crystal - 20p
2x Ceramic/Tantalum 22pF capacitors - 20p
1x 4K7-10K resistor (any will do really)
1x 10-pin Male Header/Socket (if you want a programming port on the finished board - recommended) - http://www.labombiya.com.ar/images/10pin-6pin/dsc00457.jpg
1x 28 pin DIL socket (0.3")
2x Mini LEDs Red, Green (3mm, but you can go with whatever colour you want really...)
Total: ~£8
Power
1x MAX619 DIP8 - £2.71 Farnell
1x 8 pin DIL socket
2x Tantalum 470nF capacitors - 20p
2x 22uF Low ESR electrolytic capacitors - 20p
Total: ~£3.50
IR
1x High Power IR LED (2V, 100mA) - 15p (rapid 58-0112)
1x 18R Resistor
1x 2N7000 MOSFET (or similar) - 10p
0.5m Speaker Cable (or other 2 core wire) - 50p
Total ~£0.75
Input
6x Switches SPST momentary - £1.20
1x On/Off Switch - 40p
Total ~£1.50
Light Trigger
1x Light Dependent Resistor or Photodiode - 30p
1x Potentiometer, rated the same as the maximum resistance of the LDR/diode - 10-50p
Total ~£1
Sound Trigger
1x Electret Microphone - 50p (cheap to buy in bulk on eBay)
1x 220R resistor
1x 100n capacitor (ceramic/tantalum) - 1p
1x TL072 or similar Op-Amp - 20p
1x 10uF capacitor - 10p
2x 22K resistors
1x 1K2 resistor
1x 1K resistor
1x 1M Potentiometer for varying gain (0 - 1000) 50p
Total: ~£2.00
Enclosure
1x ABS Enclosure, to fit components - your choice entirely what sort you get - 70p
1x Battery Box, dual AA - 20p
Total: ~£1
Programmer
USBasp - £12 from eBay
1x 10 pin ISP header
1x 28 pin DIL socket (0.3")
Total £12
Tools/Sundries
Soldering Iron
Solder wire
Solder sucker/Braid
Small Files
Dremel/Rotary Tool
Needle Nose Pliers
Scissors
Wire Strippers
Wires (both single and multi stranded)
Stripboard
Cost minus tools/programmer: £18 ish
Parts were bought from RapidOnline and Farnell UK. Prices are roughly accurate. I don't count resistors in the price because they are so cheap these days and per unit they're about £0.0025. It's worth coughing up for a selection anyway, they cost around £5-6 for mixtures of around 600 resistors.
If i've missed a component out but use it later in a schematic, i apologise profusely!
CPU/LCD
1x ATMega8 or ATMega88/168 (needs at least 8k of memory) £1.50
1x HD44780 compatible LCD screen £4-6 (6 if you want cool colours) from eBay
1x 10K Pot (or try different resistors) for contrast control 10-30p
1x 4MHz Crystal - 20p
2x Ceramic/Tantalum 22pF capacitors - 20p
1x 4K7-10K resistor (any will do really)
1x 10-pin Male Header/Socket (if you want a programming port on the finished board - recommended) - http://www.labombiya.com.ar/images/10pin-6pin/dsc00457.jpg
1x 28 pin DIL socket (0.3")
2x Mini LEDs Red, Green (3mm, but you can go with whatever colour you want really...)
Total: ~£8
Power
1x MAX619 DIP8 - £2.71 Farnell
1x 8 pin DIL socket
2x Tantalum 470nF capacitors - 20p
2x 22uF Low ESR electrolytic capacitors - 20p
Total: ~£3.50
IR
1x High Power IR LED (2V, 100mA) - 15p (rapid 58-0112)
1x 18R Resistor
1x 2N7000 MOSFET (or similar) - 10p
0.5m Speaker Cable (or other 2 core wire) - 50p
Total ~£0.75
Input
6x Switches SPST momentary - £1.20
1x On/Off Switch - 40p
Total ~£1.50
Light Trigger
1x Light Dependent Resistor or Photodiode - 30p
1x Potentiometer, rated the same as the maximum resistance of the LDR/diode - 10-50p
Total ~£1
Sound Trigger
1x Electret Microphone - 50p (cheap to buy in bulk on eBay)
1x 220R resistor
1x 100n capacitor (ceramic/tantalum) - 1p
1x TL072 or similar Op-Amp - 20p
1x 10uF capacitor - 10p
2x 22K resistors
1x 1K2 resistor
1x 1K resistor
1x 1M Potentiometer for varying gain (0 - 1000) 50p
Total: ~£2.00
Enclosure
1x ABS Enclosure, to fit components - your choice entirely what sort you get - 70p
1x Battery Box, dual AA - 20p
Total: ~£1
Programmer
USBasp - £12 from eBay
1x 10 pin ISP header
1x 28 pin DIL socket (0.3")
Total £12
Tools/Sundries
Soldering Iron
Solder wire
Solder sucker/Braid
Small Files
Dremel/Rotary Tool
Needle Nose Pliers
Scissors
Wire Strippers
Wires (both single and multi stranded)
Stripboard
Cost minus tools/programmer: £18 ish
Parts were bought from RapidOnline and Farnell UK. Prices are roughly accurate. I don't count resistors in the price because they are so cheap these days and per unit they're about £0.0025. It's worth coughing up for a selection anyway, they cost around £5-6 for mixtures of around 600 resistors.
If i've missed a component out but use it later in a schematic, i apologise profusely!
Step 7: Let s Get Started
I recommend going about this project (unless youre experienced) in the following way:
First off, we'll set everything out on a prototyping board (breadboard). Start off with the power supply, check it works with a multimeter and perhaps pop in an LED+resistor to make sure. Then put in a programmed AVR, wire that up, connect up your power rails and try a "hello world" blinking light application.
After you know that your programmer works, you can test a basic remote control - where turning the AVR on fires a shot every so often (this also demos the interval timer function).
Next we'll add buttons.
Then we can get onto looking at the ADC, but for this we'll need the LCD. So before we play with triggers we need to solder and connect the screen to the micro and run some simple lcd routines to make sure they're all working (there is a good application for this provided by the gentleman who wrote the libarary i'm using*). The intervalometer will also be added in a menu system.
Once that's all set up on the board and the program is working, we can start copying it over to the stripboard. The IC's will be in sockets for easy removal and it's worth adding a 10-pin header so you can program the chip whilst it's on the board. But that's getting ahead of ourselves!
*http://www.jump.to/fleury
http://homepage.hispeed.ch/peterfleury/lcdlibrary.zip
First off, we'll set everything out on a prototyping board (breadboard). Start off with the power supply, check it works with a multimeter and perhaps pop in an LED+resistor to make sure. Then put in a programmed AVR, wire that up, connect up your power rails and try a "hello world" blinking light application.
After you know that your programmer works, you can test a basic remote control - where turning the AVR on fires a shot every so often (this also demos the interval timer function).
Next we'll add buttons.
Then we can get onto looking at the ADC, but for this we'll need the LCD. So before we play with triggers we need to solder and connect the screen to the micro and run some simple lcd routines to make sure they're all working (there is a good application for this provided by the gentleman who wrote the libarary i'm using*). The intervalometer will also be added in a menu system.
Once that's all set up on the board and the program is working, we can start copying it over to the stripboard. The IC's will be in sockets for easy removal and it's worth adding a 10-pin header so you can program the chip whilst it's on the board. But that's getting ahead of ourselves!
*http://www.jump.to/fleury
http://homepage.hispeed.ch/peterfleury/lcdlibrary.zip
Step 8: Building the Power Supply
Now, this will be different depending on the route you decided to take. If you bought a MAX619 (datasheet attached), read on! Most integrated regulators will operate in roughly the same way. We have an input with a capacitor and an output with a capacitor to remove voltage ripples. The larger these capacitors are, the less ripple you'll get, but if like me you're using electrolytic capacitors then you want to keep it as small as possible.
Low ESR stands for Low Equivalent Series Resistance, you can read more about it here: http://www.low-esr.com/
http://www.illinoiscapacitor.com/uploads/papers_application/A0A0A86888CE4C0591627D18FF09455A.pdf
For this step, you'll need from your components and tools:
The MAX619
2x Tantalum 0.47uF capacitors
2x 22uF (Low ESR) Electrolytic Capacitors
Wires
When i'm wiring on breadboards i like to colour code wires, it really helps tracing circuit paths with complicated circuits and you can easily see which are ground wires etc.
The chip probably comes in antistatic packaging in a tube. Touch the exposed metal on your computer to ground yourself and then pop out the chip - it's fairly unlikely you'll actually damage a chip by touching it but it has been known to happen. The semi-circular notch indicates the top of the chip and the top left pin is pin 1. If we look at the data sheet for the MAX619 we can find both the pinout and a sample circuit.
Page 6 has the circuit we're going to implement. Note that the two tantalum capacitors join pins 1 and 8, 4 and 5. These are conveniently at opposite ends of the chip. We note that Vin goes to pin 2 in parallel with the electrolytic cap going to ground. Similarly, Vout is from pin 3 and with a parallel cap going to ground. Pin 6 should be grounded and we needn't connect pin 7.
Place the chip across the middle of the board, remember that rows are connected so we don't want to connect any pins together. Put in the ceramic capacitors, you might find it easier to run a small length of wire (my caps had very short legs) in series.
Decide which rail you want to be +5V and which +3V. I chose the outside to be +5V so that's where i wired Vout to. The battery should be wired up from one of the rails to pin 2. Remember to connect your electrolytic caps the right way round, the strip down the side generally indicates negative - it'll certainly be printed there.
Connect the red flying lead to the positive 3V rail and the black lead to the common ground.
The pictures should give you an idea of what it should look like. Be sure to test both 3 and 5 volt rails with your multimeter. If it doesn't work as expected, check to make sure you haven't accidentally crossed any wires or connected two leads by mistake. If you smell burning plastic, pull the leads out straight away, you have a couple of seconds before your chip goes bye bye =P
Low ESR stands for Low Equivalent Series Resistance, you can read more about it here: http://www.low-esr.com/
http://www.illinoiscapacitor.com/uploads/papers_application/A0A0A86888CE4C0591627D18FF09455A.pdf
For this step, you'll need from your components and tools:
The MAX619
2x Tantalum 0.47uF capacitors
2x 22uF (Low ESR) Electrolytic Capacitors
Wires
When i'm wiring on breadboards i like to colour code wires, it really helps tracing circuit paths with complicated circuits and you can easily see which are ground wires etc.
The chip probably comes in antistatic packaging in a tube. Touch the exposed metal on your computer to ground yourself and then pop out the chip - it's fairly unlikely you'll actually damage a chip by touching it but it has been known to happen. The semi-circular notch indicates the top of the chip and the top left pin is pin 1. If we look at the data sheet for the MAX619 we can find both the pinout and a sample circuit.
Page 6 has the circuit we're going to implement. Note that the two tantalum capacitors join pins 1 and 8, 4 and 5. These are conveniently at opposite ends of the chip. We note that Vin goes to pin 2 in parallel with the electrolytic cap going to ground. Similarly, Vout is from pin 3 and with a parallel cap going to ground. Pin 6 should be grounded and we needn't connect pin 7.
Place the chip across the middle of the board, remember that rows are connected so we don't want to connect any pins together. Put in the ceramic capacitors, you might find it easier to run a small length of wire (my caps had very short legs) in series.
Decide which rail you want to be +5V and which +3V. I chose the outside to be +5V so that's where i wired Vout to. The battery should be wired up from one of the rails to pin 2. Remember to connect your electrolytic caps the right way round, the strip down the side generally indicates negative - it'll certainly be printed there.
Connect the red flying lead to the positive 3V rail and the black lead to the common ground.
The pictures should give you an idea of what it should look like. Be sure to test both 3 and 5 volt rails with your multimeter. If it doesn't work as expected, check to make sure you haven't accidentally crossed any wires or connected two leads by mistake. If you smell burning plastic, pull the leads out straight away, you have a couple of seconds before your chip goes bye bye =P













Comments
Post a Comment