Archive for December, 2010

Eagle3D layout

20. December 2010 21:38 by Jens Willy Johannsen
Categories: PCB | Projects

Here is a 3D model of the board created with Eagle3D (here's a tutorial and here's some information about getting it to work on Mac OS X):

Eagle3D model of the Airsoft Rounds Counter PCB (click for larger version)

Comments 3 comments »

Firmware complete

16. December 2010 12:47 by Jens Willy Johannsen
Categories: Projects

Pretty much at least. I've added an EEPROM variable for storing the magazine capacity and I've added support for programming the magazine capacity and for resetting the rounds remaining counter without turning off the entire device.

The device can be in two modes: run and program. Run is the normal count-down-when-a-round-is-fired mode and the PRG/RST button will reset the counter (for when you put in a fresh magazine).
To enter program mode, press the PRG/RST switch while powering on the device. The display will show "PrG" for a second (as opposed to "run" in run mode) and then the counter will show 000. Pressing the PRG/RST button will increase the counter which is the magazine capacity. If the PRG/RST button is held down for a second, the counter will increase quickly. The capacity is saved in EEPROM every time is it increased so simply turn off and back on the device to use the new capacity.

Download the code here: firmware_rev2

Points of note:

  • Both the photo interrupter and the reset switch work as external interrupts (INT0 and INT1, respectively)
  • In programming mode, a 1 second timer compare match interrupt is used for hold-to-count-quickly
  • Magazine capacity is stored in EEPROM
No comments No comments »

Final layout?

12. December 2010 12:19 by Jens Willy Johannsen
Categories: ATtiny | PCB | Projects

Schematic and layout revised again. I added the 10 µF capacitor (that really ought to be there) and a LED with accompanying resistor for debugging purposes.

Here a print-it-out-and-see-if-it-fits test. And it does fit. Note that all the resistors are the same value (doesn't matter of course since all 1206 resistors are the same size) and that I've placed a 1206 resistor instead of a 1206 LED. Also, the B sized 10 µF capacitor isn't placed on the printout but it shouldn't pose any problems.

Layout, revision 3 printed 1-to-1 with components (click for larger version)

No comments No comments »

Soldering SMT components

11. December 2010 15:59 by Jens Willy Johannsen
Categories: PCB

Until now I have only made through-hole circuits.But the Airsoft Rounds Counter project's demand for a small PCB made me decide to use SMT. The components are a lot smaller so you can fit a lot more onto a small board but they are more difficult to solder by hand. (But easier and faster to assemble automatically. And cheaper.)

So I have to practise my SMT soldering. Fortunately there are a couple of great tutorials on the net. I used these:

And it's actually not that hard. I have – so far – had much success with flux-pen-and-solder-on-the-tip-of-the-iron method. A flux pen if your friend – trust me.

Here's an example of my handiwork (the ting at the top is a millimeter scale ruler):

One of the first attempts: a 1206 resistor

The resistor in the image above is a fairly large SMD component: a 1206 sized resistor. The smallest I will be using to begin with are 0805 sized capacitors and SO-20 and -24 ICs. You can see the various SMD sizes here:

Image from SMD sizes (click for larger version)

(Note: SMD means "surface-mount devices" and SMT means "surface-mount technology". I use the terms pretty much interchangeably. Which is wrong. I know.)

No comments No comments »

Schematic Revised

10. December 2010 22:05 by Jens Willy Johannsen
Categories: Projects

And a new and revised schematic for the Airsoft Rounds Counter. The only difference is that the photo interrupter's collector wire is now connected to the ATTiny's INT0 pin.

Schematic, revision 2 (click for larger version)

Update: obviously I mean "to digit cathodes" and "to segment anodes" – not the other way around.

No comments No comments »

PCB Layout Revised

10. December 2010 21:55 by Jens Willy Johannsen
Categories: PCB | Projects

Oops. The photo interrupter's collector wire wasn't connected to INT0 on the ATTiny. That has been fixed now. Also, the board is now 1 mm wider so there is enough clearing from the segment A wirepad to the edge of the board.

And the VCC and GND traces are now 24 mils and all other traces are 12 mils wide. Refer to the first layout if you can't remember what the various wirepads are for.

SMD layout, revision 2 (click for larger version)

No comments No comments »

Breadboard Prototype

10. December 2010 21:46 by Jens Willy Johannsen
Categories: Projects

Here are a couple of pictures of the breadboard setup:

The first picture is of the breadboard itself. The components are marked and the green dots are where the wires from the photointerrupter go.

Breadboard (click for larger, more legible version)

The second pictures includes the photo interrupter mounted to the barrel using duct tape (what else). Note that this was before the Max7219 and display was mounted:

Breadboard and business end of airsoft gun (click for larger version)

Comments 2 comments »

SMD layout

10. December 2010 0:53 by Jens Willy Johannsen
Categories: PCB | Projects

Here's the first draft of an SMD PCB layout for the Airsoft Rounds Counter project.

The wire pads are as follows:

  • VIN
    9V power input
  • AN, CA
    Anode and cathode the the photo interrupter LED
  • CO, EM
    Collector and emitter for the photo interrupter transistor
  • A-G, DP
    Segment anodes on the display
  • 1-4
    Digit cathodes on the display
    Reset and program tact switch

Actual size is 49.53 x 20.32 mm.

SMD layout

SMD layout (click for larger version)

Comments 1 comment »

Firmware for Airsoft Rounds Counter

9. December 2010 12:05 by Jens Willy Johannsen
Categories: Projects

Here is version 1 of the firmware for the Airsoft Rounds Counter project. The code uses avr-gcc but should be real easy to port if you use a different compiler.
This firmware is by no means the final version. Rather, it is the firmware for the breadboarded version (the schematic is here).

The ZIP archive contains three files:

  1. main.c
    Contains all the code. Please note that the code assumes that a LED is connected to PB4 which is not shown in the schematic. This is purely for debugging purposes and you can add the LED or not. The code doesn't care. And neither do I.
  2. Max7219.h
    Header file with definitions for the various Max7219 commands.
  3. Makefile
    Standard makefile. Do a 'make all' to compile, 'make fuse' to set fuses (they are set to internal 8 MHz RC oscillator with slow startup, no BOD, SPI enabled), 'make flash' to flash the ATTiny and 'make clean' to delete the binaries.

Download the ZIP archive here:

Comments 4 comments »


8. December 2010 11:06 by Jens Willy Johannsen
Categories: ATtiny | Projects

I have the first prototype of the Airsoft Rounds Counter up and running on a breadboard.
Here is the schematic. A couple of notes:

  • A 10 µF capacitor or so near the power supply certainly wouldn't hurt. Neither would a diode on the input side of the 7805.
  • The "program and reset" switch doesn't do anything with the current code and can be omitted for this version.
  • On the breadboard I also have a tact switch from the ATTiny's RESET pin to ground so I can reset the counter.
  • I'm using a 9V battery for power.

Schematic – click to view large version

Comments 3 comments »


6. December 2010 13:36 by Jens Willy Johannsen
Categories: ATtiny

The Max 7219 chip communicates using SPI. The last time I used it I had apparently written my own bit-banging SPI routine in the Arduino IDE. Which works just fine (the SPI protocol being so simple) but it seems clunky. And since the ATTiny2313 has a built-in USI (Universal Serial Interface) capable of doing SPI I thought I might as well use that.

After a bit of Googling and reading of the datasheet, this is what I came up with:

#define PIN_LOAD PB4

// Utitlity method to shift 8 bits out the USI pins in 3-wire (SPI) mode
static unsigned char spiByte(unsigned char val) 
    USIDR = val;            // Set data byte to send
    USISR = (1<<USIOIF);    // Clear Counter Overflow Interrupt Flag (by writing 1 to the USIOIF bit)
        USICR = (1<<USIWM0)|(1<<USICS1)|(1<<USICLK)|(1<<USITC);    // 3-wire mode; shift reg. clock source: external pos. edge; 4-bit counter source: USITC; Toggle USICK pin 0->1 or 1->0 (shift register will only clock on 0->1)
    } while ((USISR & (1<<USIOIF)) == 0);                        // Check for OIF set which will happen after 16 cycles. I.e. 8 bits since the clock toggles twice per bit.
    return USIDR;            // Return data value

void sendToMax7219( char reg, char value )
    PORTB &= ~(1 << PIN_LOAD);    // LOAD low
    spiByte( reg );
    spiByte( value );
    PORTB |= (1<<PIN_LOAD;    // LOAD high
No comments No comments »

Photo interrupters

6. December 2010 11:22 by Jens Willy Johannsen
Categories: General

For the Airsoft Round Counter project I need some way of detecting when a round is fired and (like most people suggest) a photo interrupter is ideal for the purpose.

Basically, a photo interrupter consists of an LED (usually an IR one) and a photo transistor. A photo transistor works like a normal transistor but instead of a current at the base, it requires light to turn on the collector-emitter current flow. In a photo interrupter the LED and the photo transistor are mounted in small posts and when nothing interrupts the beam, the transistor turns on, passing a current from the collector to the emitter. When the beam is interrupted, the transistor turns off.

A photo interrupter has four leads or connectors: anode and cathode for the LED and collector and emitter for the photo transistor.

To use it in as a digital input for a microprocessor, you can create a circuit like this:

Photo interrupter circuit for digital logic

R1 limits the current to the LED (using the normal LED calculations having obtained information about the LED forward voltage and desired current from the datasheet – typically something like 2.0V and 20 mA).
When the photo interrupter is not interrupted, the input reads 0. When an object blocks the beam, the input reads 1.

This document from Lite-On has some excellent information about using photo interrupters (after the list of their products).

No comments No comments »

Airsoft Rounds Counter

6. December 2010 10:02 by Jens Willy Johannsen
Categories: ATtiny | Projects

Browsing around on AVRfreaks' forum I stumbled upon this thread about an Airsoft Round Counter to display the number of remaining rounds when playing airsoft. This is obviously a cool idea and the thread inspired me to want to make one myself.

In the thread people discussed using everything from a simple switch attached to the trigger over photo interrupters and photo reflectors to accelerometers for detecting when a shot is fired. There is also a link to this page on the Design Decisions Wiki where a group of people have designed an "Airsoft Electronic Ammunition Counter" with detailed design considerations and requirement analysis and timing calculations. Excellent work and a great source of inspiration for this project.

My initial considerations for this project:

  1. Enclosure. Everything should be mounted to an airsoft gun and it shouldn't be the size of a shoe box!
  2. Component count. This ties in with number 1 above: if this is to be small enough to be portable I have to keep the component count down so the final PCB is of a manageable size. (Yeah yeah, "use SMD". Right, I'll do that in phase 2.)
  3. Ease of use. This will be used in skirmishes so it should be real easy to use. No re-flashing of the processor to change settings, easy to attach to a weapon and so on.

So far I have settled on the following basic components:

  1. ATTiny 2313-20PU. I could probably get by with a smaller ATTiny but since I already had a couple of these lying around I went with those...
  2. Four-digit 7 segment display
  3. Maxim 7219 CNG display driver.
  4. Wide-gap photo interrupter

Off to EAGLE for some schematic drawing and thence to the breadboard for figuring out how photo interrupters actually work...

No comments No comments »