Microcontrollers and embedded electronics

3D Gerber viewer

24. April 2012 21:45 by Jens Willy Johannsen
Categories: PCB | Tags:

This online 3D Gerber viewer is awesome.
Simply drag-and-drop your Gerber files and it'll generate a 3D model of the PCB which you can pan and zoom and save to an image.

Like this one:

Kommentarer 1 comment »

Creating custom enclosures

29. December 2011 21:04 by Jens Willy Johannsen
Categories: PCB | Tags: , ,

For another project (that I have shamefully neglected to write about) I decided I needed to put it into some kind of enclosure. And I since I couldn't find any off-the-shelf enclosures that fit I decided to make my own.
A little bit of googling let me to Shapeways that lets you create 3D printed models from your own 3D files in several different materials.

The models themselves can be created in pretty much any 3D modeling application (including the free SketchUp or Blender). I used Luxology's modo.
Obviously, exact dimensions are important. So I got PCB dimensions and coordinates from Eagle and used a caliper to measure the size of the battery and plugs and so on. And I created dummy objects for the battery and the PCB to make sure that everything fit. This is what I came up with:

Enclosure (click for larger version)

The stand-offs have 4.1 mm holes for threaded inserts to use with M3 machine screws (like these ones from RS).

And here with the dummy objects visible:

With dummy objects (click for larger version)

I exported the object to Wavefront OBJ format and uploaded it to Shapeways (I needed to rotate 90 degrees about the X-axis first in order for the preview image to render correctly on Shapeways) and specified the units as meters (since that's what modo uses by default).

The price came to €20.14 plus shipping (€8.38 for UPS shipping) for "white, strong, flexible" material. So now it's just wait and see how it turns out.

Bear in mind, that this is my first attempt at making a 3D printed object. I haven't even smoothed the edges or added support ribs or anything. Not to mention that I have only made the bottom part of the enclosure (it does however have a "lip" for mating with the top half).

For a much nicer custom enclosure, take a look at this one (also on Shapeways).


Comments 2 comments »

Filtering ADXL335 data

7. December 2011 12:07 by Jens Willy Johannsen
Categories: Projects | Tags: ,

As mentioned in my previous post, noise from the ADXL335 accelerometer readings result in flickering light. And I don't want that.

So in order to smooth the sensor data I will implement a filter. Either a simple moving-average filter or a nearly as simple low-pass filter.

(Read the entire post to see chart comparisons of the two filtering methods.)

Read the entire post »

Kommentarer 1 comment »

RGB Tumbler done

7. December 2011 9:36 by Jens Willy Johannsen
Categories: Projects | Tags: ,

I got the PCBs yesterday and promptly set about assembling one. The really interesting part of that, of course, was whether it was possible to hand-solder the LFCSP ADXL335 accelerometer ("lead frame chip scale package" – who comes up with these names?).
It turned out it was, in fact, possible. The bottom pad is not soldered, of course, and even though I had made the pads longer to be able to reach them with the soldering iron it was still kinda tricky. But it worked. In first attempt, even.

And the little tumbler thingie works perfectly! (I'll post a video.)

But I need to tweak the firmware a bit. The sensor data from the accelerometer has quite a bit of noise and this results in too flickering light. So I will need to implement a filter of some sort. Either a moving-average filter or a simple low-pass filter. And maybe also some way of "snapping" to 0, 0.5 and 1 values in order to get more "clean" colors. Stay tuned…

Kommentarer 1 comment »

RGB Tumbler design files

25. November 2011 9:38 by Jens Willy Johannsen
Categories: Projects | Tags: ,

"Information wants to be free," as we said back in the day when I was young and naïve and needed the money and a wannabe-hacker... Now, of course, we say, "open-source everything".

So here they are:
Eagle schematic and board files for the RGB Tumbler
Firmware source code

Kommentarer 1 comment »

PCB ordered

24. November 2011 9:22 by Jens Willy Johannsen
Categories: PCB, Projects | Tags: ,

The PBCs have only undergone minor revisions and Gerber files have now been sent off to ITead Studio for fabrication.

Here's a 3D rendering of both sides of the PCB (yes, several of the components were unknown to Eagle3D and I didn't bother creating custom parts). Also, the largs pads for the RGB LED are actually mostly covered by soldermask:

Eagle3D rendered PCBs (click to enlarge)


No comments No comments »

PCB layout done

19. November 2011 22:29 by Jens Willy Johannsen
Categories: PCB, Projects | Tags:

First version of the PCB for the RGB Tumbler is ready. Although usually I do through a couple of revisions before I send it off and get it made.

The PCB ended up somewhat larger that I had originally planned. It is currently 3.6 x 3.6 mm.

Here are pictures of the PCB printed in 1:1 scale and populated with components to verify that everything fits (click for larger images):

Top side (click for large version)

Bottom side (click for large version)

I need to move the slide switch a bit up to make room for the 10 µF capacitor. And I will also move the battery holder up a little bit. The NCP1402 voltage booster almost touches it. Apart from that, everything looks pretty good.

Another problem is that I ended up placing several vias directly under thethe battery. This may not be the best thing. Especially since the vias on ITead's PCBs (ITead Studio is where I get my PCBs made) are not always completely covered (even though the Gerber files say they should be covered). I guess I'll just have to cover the vias myself with some tape or something).

No comments No comments »

RGB Tumbler prototype

15. November 2011 23:40 by Jens Willy Johannsen
Categories: Projects | Tags:

Here's the prototype of the RGB Tumbler in all its glory – click to see hi-res image:

Click for large version

No comments No comments »

Prototype ready

15. November 2011 23:02 by Jens Willy Johannsen
Categories: Projects | Tags:

The prototype has been assembled and the first version of the firmware (including the no-button, Gauss-Newton calibration) is done. The calibration routine has been converted from Rolfe Schmidt's Arduino sketch to raw AVR Libc.

Here's a video of the prototype being put into calibration mode, calibrated and then turned on in normal mode.
The camera doesn't do the RGB LED credit and the colors aren't reproduced very well – probably because of the high intensity of the LED.

And I have changed the colors a bit since the last few posts. So now a white light on power-on means "if you switch off now the device will go into calibration mode on next power-up". And the blue flashing LED means "switch to next orientation and hold the sensor still". After a reading it will flash twice either green (meaning "reading ok") or red (meaning "bad reading – hold it still, man")  and lastly it will keep flashing green meaning "calibration done – cycle power".

When in normal mode, the G readings on each axis are clamped to 0-1 (I'm using the absolute value, so -1 G is the same as +1 G). This is then converted to a PWM duty cycle between 0% and 100%.
Red is the X-axis, green is Y and blue is Z. That's why it lights a solid blue when lying flat on the table: the only acceleration is 1 G on the Z-axis.

Kommentarer 1 comment »

No-button calibration

15. November 2011 11:12 by Jens Willy Johannsen
Categories: Projects | Tags:

As I explained in my last post I need to include an accelerometer calibration routine in the RGB Tumbler. But I don't want to add one or two buttons – it would take up way too much space on the PCB.
So I thought about how I could choose between "normal mode" and "calibration mode" without a keeping a button pressed on power-up and I came up with this:

On power-up, the MCU checks an EEPROM variable to see if it should go into calibration mode. If this variable is not set, the MCU sets the variable, waits one second and then clears the variable again. This means that if the user quickly (within one second) turns the device on and then immediately off, the EEPROM variable is set (because the MCU never gets to clear it before being turned off) and the next power-up will put the device into calibration mode.

For the calibration itself I will rely on delays instead of pushing a button on indicate that the device is in a new position (we need six positions for calibration). So I will flash the LED for a couple of seconds (four) and then take a reading and then repeat the process five more times. So when the LED flashes rapidly the user must change the device orientation and hold it still.
After all six readings have been taken the black magic-based calculations will be done (Gauss-Newton-based, actually but for me it amounts to the same thing) and the resulting calibration values will be stored in the EEPROM.

If this didn't make sense, then read the rest of the post to see flow charts attempting to explain the same thing.
Read the entire post »

Kommentarer 1 comment »

Accelerometer calibration

15. November 2011 10:55 by Jens Willy Johannsen
Categories: Projects | Tags:

I received a bunch of stuff yesterday – including the ADXL335 breakout board and immediately wired it up to see what kind of output I got. Everything worked nicely except the voltage on the X-axis was nearly 300 mV different from the Y-axis (!) when the accelerometer was lying flat on the table (and thus being subjected to 0 G on both X and Y). The Y- and Z-axes looked ok. Weird. And frustrating. Everything I've read about the ADXL335 (including the datasheet) led me to believe that at 0 G, the voltages on all three axes should be pretty much the same.

Anyway, after lots of Googling and scratching my head, I finally stumbled upon Rolfe Schmidt's blog where he has a lot of information about ADXL335 calibration. I don't pretend to understand the math behind Gauss-Newton algorithms but it worked perfectly!

The calibration values I got did indeed suggest that the X and Y axes were horribly miscalibrated. Maybe this particular unit is damaged – who knows. But I'll be sure to include a calibration routine in the MCU software.

No comments No comments »

Watchdog timer and idle mode

14. November 2011 11:44 by Jens Willy Johannsen
Categories: ATmega, Projects | Tags:

Since the RGB Tumbler will be running off a single coin cell I will need to make sure the ATmega microprocessor doesn't use a single microamp more than necessary. And there are several ways of doing that.

Firstly, let's switch off all peripherals that we won't be using. The "Power Reduction Register", or PRR, is the place for that (refer to section 9.11.3 of the datasheet). And also ACSR for disabling the analog comperator:
Read the entire post »

Kommentarer 1 comment »

LED Driver power dissipation

12. November 2011 13:43 by Jens Willy Johannsen
Categories: Projects | Tags:

Whenever we're dealing with more than a few miliamps we need to consider power dissipation. And for the LED Driver for the RGB Tumbler project I'm using three 50 mA LEDs connected to a SOIC-16 LED driver. That's at least 150 mA through a fairly small IC so let's look at the numbers.

The problem is always the temperature at the "junction" inside the IC. If this is too high, the IC will stop working. If it is much too high it will melt, burn or explode. And in most cases that is less than desirable.
Read the entire post »

Comments 2 comments »

RGB Tumbler parts

6. November 2011 22:54 by Jens Willy Johannsen
Categories: Projects | Tags: ,

This is what my parts search has yielded so far:


The MCU, LED driver and accelerometer will be powered directly from a 3V coin cell. But for the blue and green LEDs, 3 volts is just not enough since the LEDs require 3.2V forward voltage and the LED driver requires a further 0.4V for the current regulator. So about 4V would be perfect. I've used the NCP1400 DC-DC converter before but it provides only 100 mA and since each LED draws 50 mA I will need more than that. The NCP1402, however, provides up to 200 mA. And even though it exists in a 4.0V version I've been unable to find that in stock anywhere so I'll probably go with a 5V version instead.
Note that with 5V I can't run multiple LEDs in series so I'll either have to make do with a single LED or use parallel LEDs.


This one looks nice: Optek OVSTRGBB1CR8. It's an RGB LED with 50 mA per LED in a PLCC6 package. PLCC are pretty easy to hand-solder even though the leads are bent in under the device. Flux and a tinned tip will do the trick. And it has a generous 2.1 mm pitch.


I use AVR MCUs because that's what I'm familiar with. I will need three PWM channels and three A/D channels. This AVR Quick Reference gives a good overview of the features of the various ATtiny, mega and Xmega devices.

The ATtiny's have either not enough PWM or A/D channels or not enough pins to utilize both PWM and A/D at the same time or they only come in SOIC-14 packages (too big) or QFN-20 (don't wanna). The ATmega168 and -328 however have enough PWM and A/D channels and plenty pins to use both at the same time and come in a TQFP-32 package. And I already have a couple of TQFP '328s laying around so an ATmega 328P-AU it is.

LED Driver

This one was tricky. First off: do I even need an LED driver? Driving the LEDs directly from the MCU ports is not possible because the MCU cannot supply enough current for three 50 mA LEDs (and the MCU is running at too low a voltage in this case). I could have the MCU drive MOSFETs and use current-limiting resistors. But that would waste too much power in the resistors and the whole thing would eat up quite a bit of PCB real estate. So yes: I need a driver.

I spent several hours looking at and dismissing LED drivers because they were BGA or LGA package only or they were for low-current LEDs only (10 or 25 mA) or they had no current regulation at all. This one looked promising: ON Semi NCP5623. It's an I2C controlled three channel 175 mA controller in a TSSOP package. I don't even need to use PWM from the MCU. However, I only found it at Digi-Key where it was "non-stocked" with a minimum quantity of 2500 pcs. I don't think so.

I finally settled on this one: On Semi CAT4109. It doesn't inlcude PWM controller so the MCU will have to do the PWM'ing. Also it needs three resistors instead of only one for setting the current and it exists only in a SOIC-16 package which is kinda bulky. But it will have to do.


For the last critical part I settled on an Analog Devices ADXL335. It is +/- 3G with three analog outputs. The only problem is that it is in a LFCSP-16 package which is intended for reflow soldering only. It may have visible connections on the side of the package. I hope it does because then it should be possible to hand-solder it. I will make sure the pads extend 0.5 to 1 mm beyond the case itself so I can flux-and-tinned-tip solder it. I'll keep my fingers crossed and we'll see. Note that the chip has a pad on the bottom (which is impossible to hand-solder) but according to the datasheet, the "exposed pad is not internally connected but should be soldered for mechanical integrity". Which means that I should be able to get away with not soldering it.


No comments No comments »

RGB Tumbler

5. November 2011 19:40 by Jens Willy Johannsen
Categories: Projects | Tags: , ,

For a new project I thought I'd like to do some RGB LED stuff. And I got the idea of making a small circuit (small enough to be put inside a ping-pong ball) that controlled one (or more) RGB LEDs from its physical orientation. Simply put, acceleration on the X, Y and Z axes would translate to intensity of the red, green and blue LED. And since there is always an acceleration of 1G towards the center of the Earth (also known as "gravity"), the color of the RGB LED would depend on its orientation.

So the system will comprise the following "blocks":

  1. Power
  2. MCU
  3. 3 axis accelerometer
  4. LED driver. Or maybe the LEDs can be controlled directly from the MCU.
  5. RGB LED. One or two.

When looking for parts there's a few things to keep in mind:

  1. Size matters. It must be as small as possible. I only recently started working with SMD components but already I can't imagine having to deal with those clunky, Soviet tractor-like through-hole components. But I still haven't gone all the way into reflow soldering. So I'm looking for primarily SOIC, TSSOP and TQFP packages. QFN packages are actually possible to hand-solder but it's not fun at all and LGA and BGA are pretty much impossible. For many-pin devices, SOIC is actually kinda big with their huge 1.27 mm pitch and 0.5 mm pitch TSSOP would be preferable.
  2. Power. As little as possible. I plan on running everything off a single 3V coin cell so voltage should be 2.7 to 3 volts and amps should be as few as at all possible.
  3. Availability. I much prefer to be able to get everything from one, local provider.

So off to the intertubes for some Googling…

Comments 2 comments »

Airsoft Rounds Counter follow-up

1. November 2011 22:07 by Jens Willy Johannsen
Categories: Projects | Tags:
So here is the looong overdue follow-up of my Airsoft Rounds Counter project. Sorry for the incredible long wait…

Ok. Everything has been assembled and mounted. I mounted the control unit inside the stock. Power comes from the gun battery. The wires from the photo interrupter to the control unit run inside the handguard and upper receiver. I have drilled through the aluminium endblocks of the handguard and I'm using three pin RC servo type plugs and sockets to connect the silencer with the IR photo transistor and LED to the gun (I need to unplug it in order to unscrew it) and also inside the stock so I can easily disassemble the gun.

And it works pretty darn good, actually!

Comments 2 comments »

High-power LED driver

10. June 2011 10:09 by Jens Willy Johannsen
Categories: PCB

For the airsoft flag device project I have been working with normal or semi-high-powered LEDs. And they just weren't bright enough so I had to step it up to proper 1W or 3W LEDs.

However, that required me to change the circuits since it's not a good idea to drive a 1W LED with a series resistor since way too much power will get wasted in the resistor. The correct way of driving high-power LEDs is with a dedicated constant current circuit.

So I decided to make a small board with just the driver circuit and external leads for power, GND and LED control as well as leads for the LED itself.

Of all the many ways of making a constant current regulator, I decided to go with the ZXLD1350 IC for reasons I can't remember.

Here are links to the datasheets:
ELL5PS SMD inductor
ZLLS1000 SMD Schottky diode

EAGLE files: Eagle files

Terms of use: Yeah sure, go ahead. No guarantees, obviously.

Comments 3 comments »

Board assembled

6. January 2011 13:38 by Jens Willy Johannsen
Categories: PCB, Projects | Tags: ,

I have received the PCBs and assembled the first one. Everything expect connections to external components, that is.

Here's a picture. It looks pretty much like the rendering. And I know: some of the soldering joints could have been made better but bear in mind that this is my first SMD soldering attempt so I'm actually pretty pleased with the result.

PCB with all SMD components (click for larger version)

Comments 6 comments »

Eagle3D layout

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

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 | Tags: , , ,

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 | Tags: ,

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 | Tags:

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 | Tags:

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 | Tags: ,

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 | Tags: ,

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 | Tags: ,

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)

Kommentarer 1 comment »

Firmware for Airsoft Rounds Counter

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

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 | Tags:

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 | Tags: , ,

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 | Tags: ,

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 »