Week11 - Input Devices

Lecture Notes

I’ve updated Week11’s lecture notes in my gitbook .

Assignment

measure something: add a sensor to a microcontroller board that you have designed and read it

In my assignment, I use Attiny45. It’s necessary to figure out the pin number before board design:

Design the board

The schematic:

The board:

Check with ERC and DRC. I set 16mil for clearance to avoid milling problem

Then export the board to monochrome png file (I use 800 dpi resolution) for milling:

You can download the eagle sch & brd & png files here.

Make the board

I use Roland SMR-20 to mill my board.

In fabmodules, I set the input png dpi to 1500 and calculate the milling path:

1/64 endmill for traces and 1/32 endmill for interior.

After soldering:

Program

I use Arduino to program the board to do something.

Don’t forget to set board type and choose the right processor. And pay attention to the clock setting. Attiny45 uses 8 MHz internal clock.

Below is my code and you can download them here.

1

  // set pin numbers:
const int buttonPin = 3;     // the number of the pushbutton pin
const int ledPin =  4;      // the number of the LED pin

// variables will change:
int buttonState = 0;         // variable for reading the pushbutton status

void setup() {
  // initialize the LED pin as an output:
  pinMode(ledPin, OUTPUT);
  // initialize the pushbutton pin as an input:
  pinMode(buttonPin, INPUT);
}

void loop() {
  // read the state of the pushbutton value:
  buttonState = digitalRead(buttonPin);

  // check if the pushbutton is pressed.
  // if it is, the buttonState is HIGH:
  if (buttonState == HIGH) {
    // turn LED on:
    digitalWrite(ledPin, HIGH);
  } else {
    // turn LED off:
    digitalWrite(ledPin, LOW);
  }
}

In this case, the phototransitor works as a button. It turns the LED on and off:

A light board

I made a board with a light sensor:

Then download hello.light.45.c and makefile. cd into the folder in terminal, run make command: make -f hello.light.45.make

it responds

1
avr-gcc -mmcu=attiny45 -Wall -Os -DF_CPU=8000000 -I./ -o hello.button.45.out hello.button.45.c
avr-objcopy -O ihex hello.button.45.out hello.button.45.c.hex;\
	avr-size --mcu=attiny45 --format=avr hello.button.45.out
AVR Memory Usage
----------------
Device: attiny45

Program:     364 bytes (8.9% Full)
(.text + .data + .bootloader)

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

Next command: sudo make -f hello.light.45.make program-usbtiny

It responds:

1
avr-objcopy -O ihex hello.light.45.out hello.light.45.c.hex;\
	avr-size --mcu=attiny45 --format=avr hello.light.45.out
AVR Memory Usage
----------------
Device: attiny45

Program:     502 bytes (12.3% Full)
(.text + .data + .bootloader)

Data:          1 bytes (0.4% Full)
(.data + .bss + .noinit)


avrdude -p t45 -P usb -c usbtiny -U flash:w:hello.light.45.c.hex

avrdude: AVR device initialized and ready to accept instructions

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

avrdude: Device signature = 0x1e9206
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 "hello.light.45.c.hex"
avrdude: input file hello.light.45.c.hex auto detected as Intel Hex
avrdude: writing flash (502 bytes):

Writing | ################################################## | 100% 0.74s

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

Reading | ################################################## | 100% 0.94s

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

Then download hello.light.45.py.

Connet my board with USB to TTL and run:

python hello.light.45.py /dev/ttyUSB0

It responds:

1
Traceback (most recent call last):
  File "hello.light.45.py", line 62, in <module>
    ser = serial.Serial(port,9600)
  File "/Library/Python/2.7/site-packages/serial/serialutil.py", line 180, in __init__
    self.open()
  File "/Library/Python/2.7/site-packages/serial/serialposix.py", line 294, in open
    raise SerialException(msg.errno, "could not open port %s: %s" % (self._port, msg))
serial.serialutil.SerialException: [Errno 2] could not open port /dev/ttyUSB0: [Errno 2] No such file or directory: '/dev/ttyUSB0'

I google this problem: Failed to open port /dev/ttyUSB0 - ROS Answers: Open Source Q&A Forum

My classmate told me that maybe because I didn’t have FTDI drive.

I use this command to check my port ls /dev/tty*. There’s no /dev/tty.usbserial-A400gwhT exist.

So I tried install D2XX Direct Drivers and Virtual COM Port Drivers. Still didn’t work.

Then I google this article How to Install FTDI Drivers - learn.sparkfun.com and reinstall the drive. After restarting the computer, I plug FTDI2USB device, /dev/tty.usbserial-A400gwhT appeared in the list.

Next, I tried to open py file with this command:

python hello.light.45.py /dev/tty.usbserial-A400gwhT 9600

Still got something wrong:

command line: hello.light.45.py serial_port

I looked into the python program, and figured out to change the len(sys.argv) from 2 to 3:

1
if (len(sys.argv) != 3):
   print "command line: hello.light.45.py serial_port"
   sys.exit()
port = sys.argv[1]

Problem fixed:

Video

A switch board

I also made a board with a button:

Download hello.button.45.c and makefile. cd into the folder in terminal, run command: make -f hello.button.45.make and sudo make -f hello.button.45.make program-usbtiny. All going well.

Connect my board to TTL2USB:

Next, I run python term.py /dev/ttyUSB0

It responds:

command line: term.py serial_port speed

I forgot to change my port name to :

python term.py /dev/tty.usbserial-A400gwhT 9600

And the result came out:

Video

A temperature board

I continued to make a board with a temperature sensor:

Download hello.temp.45.c and makefile. Used make command to make the board.

Then run the program: python hello.temp.45.py /dev/tty.usbserial-A400gwhT 9600