Wednesday, October 30, 2013

Halloween pumpkin LEDs and IR project

What's Halloween without a carved pumpkin? And what's the point of lighting the pumpkin with a candle when we have LEDs?


The LEDs flicker for 30s when someone is close to the pumpkin. Notice the IR-detector in the nose




In this post I will show how I built my pumpkin powered with a ATtiny85 microcontroller, some LEDs and an IR detector.


Firstly, I soldered 10 LEDS in two groups of five, driven by BC547 transistors. The transistors are driven by two pins on the ATtiny85. A third pin on the ATtiny is used to read a IR sensor (similar to the Parallax PIR). The electronics is driven by three 1.5V AA batteries.


10 LEDs driven by ATtiny85. The IR electronics is wrapped in plastics to protect it from pumpkin juice.

 

The purpose of the code is to let the LEDs flicker for 30s when the IR sensor is activated. The code is super simple and should not need any further comment. 

int led = 1;
int led2 = 0;
int pirPin = 2;
int calibrationTime = 10;

void setup() {               
  pinMode(led, OUTPUT);
  pinMode(led2, OUTPUT); 
  pinMode(pirPin, INPUT);
  digitalWrite(pirPin, LOW);
  for(int i = 0; i < calibrationTime; i++){
      delay(1000);
  }
}

void loop() {
  if(digitalRead(pirPin) == HIGH){
    for(int i=0;i<300;i++){
      analogWrite(led, random(120)+135);
      analogWrite(led2, random(120)+135);
      delay(100);
    }
  }
  else
    digitalWrite(led, LOW);
    digitalWrite(led2, LOW);

}


The LEDs are PWM driven with some random functions inspired by the Realistic Flickering Flame Instructable. The ATtiny was programmed using the Arduino IDE and my DIY programmer.


Once the electronics is completed it is time to carve the pumpkin. I used a template I found on the Internet to create the scary(?) face. It sure helped a lot.



After the pumpkin is carved and the face is completed, the pumpkin is washed, and the electronics is fitted. The IR detector was fitted in the nose of the face.

Once the pumpkin head is assembled, you might want to roast the pumpkin seeds. If you threw them away, you can enjoy a cup of tea instead. I followed this recipe and was fairly happy with the result.


Roasted pumpkin seeds (or what's left of them anyway)


Tuesday, October 22, 2013

Arduino ATtiny programmer circuit

In several small microcontroller projects there is a need for just a few pins and only a few KB of code. Atmels series of ATtiny microcontroller serves this purpose and can be programmed using the Arduino IDE.

An ATtiny such as the ATtiny85 can not be connected directly to a computer via a FTDI cable but needs an Arduino (i.e., a ATMega328) in between. I built this little circuit which connects the SCK, MISO, MOSI and RESET of a ATTiny to an ATMega using the latter as an ISP. This small prototyping unit also has some header pins and a small solderless breadboard for prototyping circuits for the ATtiny.


The prerequisite to run this board is that the ATmega328 is preloaded with the Arduino bootloader. You also need to prepare the Arduino ISP for ATtiny microcontrollers.
  1. Download the zip file from GitHub.
  2. Unzip the zip-file and place the attiny folder under a "hardware" folder in your Arduino sketchbook directory.
  3. You should now see the ATtiny entries under the board menu in the Arduino ISP.
  4. Connect the FTDI adapter (as this one from Sparkfun).
  5. Select the Arduino Uno from the board menu.
  6. Upload the ArduinoISP sketch from the examples menu (you only have to do this once).


After that, you are ready to program the ATtiny. Follow this procedure:
  1. Open the Blink sketch from the examples menu.
  2. Change the pin number for the LED from 13 to 0.
  3. Select the ATtiny85 from the tools > board menu.
  4. Select "Arduino as ISP" from the tools > programmer menu.
  5. Upload the sketch and watch the LED (you just connected at pin 0 on the ATtiny85) blink.
The circuit diagram for my ATtiny-programmer board is shown below.



Female headers are used to break out all the pins on the ATtiny. I also added a DIP switch (not in the diagram) do disconnect the ATMega328 from the ATtiny once it is programmed.


More information about ATtiny Arduino stuff can be found in the links below:




The board can also be used to bootload an ATMega328 by connecting the SCK, MISO, MOSI and RESET pins to the female headers as shown in the above picture. In this case the ATtiny must be removed. You must also add a 16MHz crystal and 22pF decoupling capacitors as well as a 10K pullup on the reset pin. The schematics for such a bootload circuit is shown here.