Week 6 - Electronics Design

Assignment


Redrawing the echo hello-world board in KiCad

KiCad is a multi-platform, open source software for electronic design which supports schematic entry and PCB layout. I used KiCad only once for a very very small circuit in 2008 - since then it has changed and evolved a lot!

Once I opened KiCad, I was lost in the UI and didn't know how to start. I googled a bit and I stumbled across a really nice video tutorial: KiCAD Quick-Start Tutorial by Windsor Schmidt. It's a 20-minutes life-saver video which shows how to design (almost) any circuit with KiCad. I followed the tutorial step by step, with the video running on a second screen (and pausing it frequently). In about one hour my first test design was ready and I started drawing the hello-world board. Not so bad! :-)

KiCad is technically a set of applications: when you open the main program, you are presented a simple interface. First create (or load) a new project, then you can work on it switching between the various modules: the schematic editor, the pcb editor, the library editors for schematic or footprint components, a simple electronic calculator etc.

I opened EESchema - Electronic schematic editor (the first big button) and, before drawing anything, I imported the fab library. I suggest you to do the same: from Preferences --> Component Libraries click Add and then select fab.lib

Using the hello-world board PCB digram as a reference, I designed from scratch the electronic schematic. You can see the big yellow box on the right with 14 pins (the ATtiny44A), the FTDI connector on the right and a few components in the middle, all of them connected through a net of green paths.

The complete schematic of the hello-board is show below: as you might have noticed, I have not drawn all paths. In some cases I used labels (GND, VCC, TX, RX) to connect the points virtually. I have found labels really useful for three main reasons: they reduce the amount of visible paths, they allow you to scatter on the sheet parts of the schematic that are logically separated and connect them easily, and they are useful to name and differentiate paths when you are drawing the PCB.

When the schematic was complete, I run a bunch of functions from the Tools menu before moving on to the PCB layout:

  1. Annotate schematic - needed to automatically number all the components (let's say you have two resistors: R? and R? will become two unique resistors R1 and R2)
  2. Assign component footprint - this step is useful to assign each logical component in the schematic to a physical footprint. So, if you need to use a component for some reason (for example because the standard one is out of stock), you just have to assign the new footprint to the component. (Remember to save before exit)
  3. Generate Netlist file - generates a list of the components and of all the wirings between them

At this point, I moved to Pcbnew, the printed circuit board editor. First of all, I imported my fab.pretty footprint library. I have improved to original fab.mod library by associating 3D design to the elements needed for this assignment. If you want to use KiCad and have nice PCB models, you can download my fab.pretty library and import it in Pcbnew through Preferences --> Footprint Library.

Then I set the global design rules from the Design Rules menu. Specifically I set:

Now, from Tools --> Netlist I read the Netlist previously generated by EESchema. The tool loads all the components and the links; however, the imported components are all overlapping and the tracks need to be physically traced on the board. I placed all the components on the PCB in the right place, then I drew all the tracks and, finally, I added a border cut trace and defined a margin for the board.

A simple animation of the steps is shown below:

The 3D model of the basic hello-world board rendered by the KiCad 3D Viewer is quite nice: If you want, you can download the basic hello world KiCad project

At this point, I went back the Eeschema and I added a button and a LED. Note that I added a resistor in series to the LED to limit the current flowing out of the IC pin; I also added a pull-down resistor to the pin connected to the button (a brief overview of pull-up and pull-down resistors can be found on the Arduino wiki - :: Pull Up and Pull Down Resistors ::).

As you can see, I used two new labels (BTN and LED) to connect the two new components to the pins of the ATtiny44 PA3 and PB2 respectively. This allowed me to draw the two edits of the original hello-world on the bottom-left part of the page and connect them to the rest of the circuit without drawing additional wires.

As described above, I repeated the Annotate schematic, Assign component footprint and Generate Netlist file steps. Then, I moved on to Pcbnew and I re-read the Netlist. This last step added the new components on the PCB, so I had to place them and to create the new tracks. Eventually, I added my name on the bottom-left empty part of the copper layer using a few tracks.

The 3D model of the modified hello-world board (kicad project here) rendered by the KiCad 3D Viewer follows:

From KiCad to the milling machine

Exporting the PCB for the milling machine was the most difficult part. I didn't expect that, but I managed to find a way to generate all the files and I'm writing down (I'm a KiCad newbie - if you know a better solution please contact me!).

Pcbnew can export to SVG and plot the layers as PDF, PS, SVG, DXF or Gerber files. Since fabmodules supports PNG, SVG and STL formats, I tried to plot to SVG, but the output included all the working area and not only the area withing the margins of the PCB . So I tried to export to SVG directly: the KiCad export options includes the "Board area only" option, so I checked them and exported the PCB to SVG. Then I opened it in fabmodules and everything went fine, but the outcome of the calculate process was full of errors: straight lines were not straight at all. So, I tried to export the layers as Gerber files, but the GerbView tool included in KiCad can only open Gerber files but not export them. I installed the alternative free viewer Gerbv, which is able to export the layers as PNG. Gerbv imported the layer smoothlessly, so I gave it a try. I exported the layer as PNG, but again the milling path computed by fabmodules was noisy, that is without straight lines.

I looked again at my PNG and SVG files: they were both quite small (about 200x200px). I looked for an Export Resolution option in Gerbv and Pcbnew, but I wasn't able to find any resolution settings. Zooming the small PNG blurred it as expected, so I abandoned PNG and moved back to SVG. The vector image exported by Pcbnew was really small (143x158 pixels) but, since it is a vectorial graphic, I was able to zoom the preview without loosing quality.

fabmodules and SVG
Apparently fabmodules supports SVG vector images, but it does not rescale them nicely.

I opened the SVG in Inkscape and again I zoomed: the tracks were nice. So I exported the SVG as an high-resolution (2000 dpi) PNG image using Inkscape.

I uploaded the new high-resolution PNG to fabmodules, I set all the parameters used to mill the FabISP (see week4 documentation) plus the "Invert image" option and... I obtained the correct milling path!

I saved the PCB and the cutout RML files, I moved to our Roland MDX-40 milling machine and I run the milling process (again, see week4 documentation for knowing how to setup the machine). The milling process took about 40 minutes, but everything went fine This is the board next to a 1 Euro coin, just to have a better idea of its actual size. The surface of the PCB was quite wrinkled, so I used sandpaper to smooth the copper layer - it improved a bit, but it's not perfect. Cristiana, who cut her PCB after me, had a lot of issues, so I consider myself quite lucky. However, we believe that there are some issues with the endmill. We plan to do some tests and, if needed, mill the PCB again. Before moving to soldering, I opened the photo of the PCB in GIMP and I added the PNG of the traces as a transparent layer. Then, I rescaled and moved it to overlap the traces on the copper. As you might have already spotted, two tracks were not cut - they are still joined and short-circuited. I checked fabmodules and I found that the milling path is not actually cutting this small segment - after a discussion with Fiore, we came to the conclusion that I set wrong Design Rules, specifically a clearance value too low for the cutting machine - next time I will increase clearance from 7 to 20 mills.

Before soldering the components, I tried to separate the two tracks using a cutter. Not a great idea, but worth a try before milling again the PCB.

Then I completed the board but, when checking the correctness of my soldering with a multimeter, I discovered that the GND and MOSI pins of the pin header were short-circuited. So I double checked the photo of the PCB and noticed that the copper was not removed properly. I de-soldered the pin header, cut the paths, re-soldered the pin and... the board was finally ready to be programmed.

I found a pair of old flat cables used for serial ports: so I cut both and soldered the wires in order to obtain a Female-Female SPI cable for programming the board using my FabISP.

CONNECTING THE HELLO BOARD
In order to program the hello board you have to:
  1. Connect the FabISP to your PC through USB port
  2. Connect the FabISP to the Hello board through the SPI (i.e. the six pins)
  3. Connect the GND and VCC pins of the Hello board (those next to RX and TX pins) to GND and 5V respectively (you can use a battery, an external power supply or the pin of an Arduino)
  4. Run the makefile to program the board

I created I test program - a simple blink.c code for blinking the LED.

PROJECT=blink
SOURCES=$(PROJECT).c
MMCU=attiny44
F_CPU=20000000

CFLAGS=-mmcu=$(MMCU) -Wall -Os -DF_CPU=$(F_CPU)

$(PROJECT).hex: $(PROJECT).out
	avr-objcopy -O ihex $(PROJECT).out $(PROJECT).c.hex;\
	avr-size --mcu=$(MMCU) --format=avr $(PROJECT).out
 
$(PROJECT).out: $(SOURCES)
	avr-gcc $(CFLAGS) -I./ -o $(PROJECT).out $(SOURCES)

clean:
	rm -f $(PROJECT).c.hex $(PROJECT).out
 
program-usbtiny: $(PROJECT).hex
	avrdude -p t44 -P usb -c usbtiny -U flash:w:$(PROJECT).c.hex

program-usbtiny-fuses: $(PROJECT).hex
	avrdude -p t44 -P usb -c usbtiny -U lfuse:w:0x5E:m

And I compiled and uploaded it using the blink.make Makefile.

#include <avr/io.h>
#include <inttypes.h>
#include <util/delay.h>

#define F_CPU 20000000UL        /* 20MHz crystal oscillator */

#define LED_PORT PORTB          /* PORTx - register for LED output */
#define LED_BIT PB2             /* bit for LED output */
#define LED_DDR DDRB            /* LED data direction register */

#define BLINK_TIME 1000         /* blink delay */

int main(void)
{
	LED_DDR = _BV(LED_BIT); 
	LED_PORT |= _BV(LED_BIT);

	while (1) {
		_delay_ms(BLINK_TIME);
		LED_PORT ^= _BV(LED_BIT);
	}
}

and the commands:

$ make -f blink.c.make 
avr-gcc -mmcu=attiny44 -Wall -Os -DF_CPU=20000000 -I./ -o blink.out blink.c
avr-objcopy -O ihex blink.out blink.c.hex;\
avr-size --mcu=attiny44 --format=avr blink.out
AVR Memory Usage
----------------
Device: attiny44

Program:     142 bytes (3.5% Full)
(.text + .data + .bootloader)

Data:          0 bytes (0.0% Full)
(.data + .bss + .noinit)



$ sudo make -f blink.c.make program-usbtiny-fuses 
avr-objcopy -O ihex blink.out blink.c.hex;\
avr-size --mcu=attiny44 --format=avr blink.out
AVR Memory Usage
----------------
Device: attiny44

Program:     142 bytes (3.5% Full)
(.text + .data + .bootloader)

Data:          0 bytes (0.0% Full)
(.data + .bss + .noinit)


avrdude -p t44 -P usb -c usbtiny -U lfuse:w:0x5E:m

avrdude: AVR device initialized and ready to accept instructions

Reading | ################################################## | 100% 0.00s

avrdude: Device signature = 0x1e9207 (probably t44)
avrdude: reading input file "0x5E"
avrdude: writing lfuse (1 bytes):

Writing | ################################################## | 100% 0.01s

avrdude: 1 bytes of lfuse written
avrdude: verifying lfuse memory against 0x5E:
avrdude: load data lfuse data from input file 0x5E:
avrdude: input file 0x5E contains 1 bytes
avrdude: reading on-chip lfuse data:

Reading | ################################################## | 100% 0.00s

avrdude: verifying ...
avrdude: 1 bytes of lfuse verified

avrdude: safemode: Fuses OK (E:FF, H:DF, L:5E)

avrdude done.  Thank you.



$ sudo make -f blink.c.make program-usbtiny
avr-objcopy -O ihex blink.out blink.c.hex;\
avr-size --mcu=attiny44 --format=avr blink.out
AVR Memory Usage
----------------
Device: attiny44

Program:     142 bytes (3.5% Full)
(.text + .data + .bootloader)

Data:          0 bytes (0.0% Full)
(.data + .bss + .noinit)


avrdude -p t44 -P usb -c usbtiny -U flash:w:blink.c.hex

avrdude: AVR device initialized and ready to accept instructions

Reading | ################################################## | 100% 0.00s

avrdude: Device signature = 0x1e9207 (probably t44)
avrdude: NOTE: "flash" memory has been specified, an erase cycle will be performed
         To disable this feature, specify the -D option.
avrdude: erasing chip
avrdude: reading input file "blink.c.hex"
avrdude: input file blink.c.hex auto detected as Intel Hex
avrdude: writing flash (142 bytes):

Writing | ################################################## | 100% 1.13s

avrdude: 760 bytes of flash written
avrdude: verifying flash memory against blink.c.hex:
avrdude: load data flash data from input file blink.c.hex:
avrdude: input file blink.c.hex auto detected as Intel Hex
avrdude: input file blink.c.hex contains 760 bytes
avrdude: reading on-chip flash data:

Reading | ################################################## | 100% 1.33s

avrdude: verifying ...
avrdude: 760 bytes of flash verified

avrdude: safemode: Fuses OK (E:FF, H:DF, L:5E)

avrdude done.  Thank you.
WARNING
Do not play with fuses unless you really, really know what you are doing.

Later, while playing with the ATTiny, I messed up and bricked mine by setting wrong fuses value - specifically, I set the RSTDISBL bit, which disables further reprogramming of the chip with the FabISP.

Dumb mistake: it worked for the first time, but... I wasn't able to program the device anymore... :-(

So I have learned RSTDISBL lesson the hard way - it's unlikely I will make this mistake a second time. I will never play randomly with fuses anymore!

To solve the problem, I would need to take away the chip and perform an High Voltage Serial Reprogramming using a dedicated circuit. I will build it someday but, in the meantime, I changed the ATTiny44a using the SMD rework station available at FabLab Mantova

Download zone - List of files