Input Devices


Introduction

This week our assignments are the following:
• Probe an input device's analog levels and digital signals (group)
• Measure something: add a sensor to a microcontroller board that you have designed and read it (Individual)

Preparation


I decided to make push button, hall sensor and temperature sensors boards. My first step was to make a list of necessary components.

Components for the push button board

• ATtiny 45 microcontroller
• 10k SMD 1206 Resistor
• 1uF SMD 1206 Capacitor
• B3SN-3112P Push button
• 1x6 male pin headers
• 2x3 male pin headers

Components for hall sensor booard

• ATtiny 45 microcontroller
• 10k SMD 1206 Resistor
• 1uF SMD 1206 Capacitor
• Hall effect sensor
• 1x6 male pin headers
• 2x3 male pin headers

Components for temperature sensor booard

• ATtiny 45 microcontroller
• 10k SMD 1206 Resistor
• 1uF SMD 1206 Capacitor
• Temperature sensor
• 1x6 male pin headers
• 2x3 male pin headers

I also needed Fab ISP and FTI Cable for this assignment.
About Fab ISP you can read at week5
The FTDI cable is a USB to Serial (TTL level) converter which allows for a simple way to connect TTL interface devices to USB. The I/O pins of this FTDI cable are configured to operate at 5V. The FTDI cable develops, manufactures, and supports devices and their related software drivers for converting RS-232 or TTL serial transmissions to USB signals, in order to allow support for legacy devices with modern computers. FTDI provides application-specific integrated circuit (ASIC) design services. They also provide consultancy services for product design, specifically in the realm of electronic devices.
ATtiny 45 is high-performance, low-power Microchip 8-bit AVR RISC-based microcontroller combines 4KB ISP flash memory, 256-Byte EEPROM, 256B SRAM, 6 general purpose I/O lines, 32 general \ purpose working registers, one 8-bit timer/counter with compare modes, one 8-bit high speed timer/counter, USI, internal and external Interrupts, 4-channel 10-bit A/D converter, programmable watchdog timer with internal oscillator, three software selectable power saving modes, and debugWIRE for on-chip debugging. The device achieves a throughput of 20 MIPS at 20 MHz and operates between 2.7-5.5 volts. By executing powerful instructions in a single clock cycle, the device achieves throughputs approaching 1 MIPS per MHz, balancing power consumption and processing speed.
You can download the datasheet of ATtiny 45. When I took all componets I downloaded all png files, opened fab mudules and started preparing files for cutting.
steps

Setting up the machine


• Turn on the machine
• Put the material in the machine, fixed it with double sided stick
• Attached end mill 1/64 for milling traces
• Opened fab modules and upload my files there, then saved
• Setted the origin point chose"[Machine Coordinate System" in our case will use "G54" and "G55" that's the first 2 of 6 Work Coordinate Systems. During this process I used Vpanel pointers "right" "left" "up" "down"



Picture of endmills I used





• Went back to fab modules and chose image "png"- Gcode "nc"-PCB traces "1/64" - "selected calculate" - "save". When the files had been saved I sent them to machine for cutting. Started the cutting process and paused to check how it was going on. After I continued the cutting. I repeated above mentioned activities 3 times for all boards. For cutting out the boards I opened fab modules chose image "png"- Gcode "nc"-PCB interior "1/32" - "selected calculate" - "save". Then I cleaned all boards and started soldering process.

all boards

Board for the measurment of magnetic field


magnetic filed board

When the board was ready I started programing it and had some problems with Windows 10 and decided to use another computer with Linux (Babken's computer). For programing the board I needed "C" codes, makefiles and python program to read the data from the keynotes for single board. I created separate folders for them "Nutton folder", "Hall sensor folder" "Temperature sensor folder".

Programming Hall sensor board

I connected Fab ISP to the board, then FTDI cable connected to the same board and connected them to the computer by USB port. The board has 6 pins for connecting fab ISP and 6 pins FTDI. Opened "Hall sensor folder" by right clicked
Open In terminal
and typed the command
"make -f hello.mag.45.make program-usbtiny" and pressed enter. "program-usbtiny" specifies the programmer we're going to use change "usbtiny" to "avrisp2" or "dragon_isp" in case of we used that programmers
After typed "avrdude -p t45 -P usb -c usbtiny -U flash:w:hello.mag.45.c.hex"" command end pressed enter and then typed "sudo python hello.mag.45.py /dev/ttyUSB0 9600"command.
After the programming I used magnets for measuring the magnetic field.
As I used FTDI, I changed the /ttyUSB0 to /ttyACM0
magnets

measuring the magnetism


Board for the measurment temperature


After cutting and soldering the board I tested it using tester and found out that I did not solder all components properly and desoldered some components and soldered them again. The board has 6 pins for connecting fab ISP and 6 pins FTDI. The final attempt was succesful and the board was ready for programming. Connected Fab ISP and FTDI cable to the board then all them connected to the computer by USB port and opened the tepmerature folder by right click selected open in terminal function.
Typed the following commands


• "make -f hello.temp.45.make program-usbtiny" "program-usbtiny" specifies the programmer we're going to use change "usbtiny" to "avrisp2" or "dragon_isp" in case of we used that programmers
• "avrdude -p t45 -P usb -c usbtiny -U flash:w:hello.temp.45.c.hex"
• "sudo python hello.temp.45.py /dev/ttyUSB0 9600"
As I used FTDI, I changed the /ttyUSB0 to /ttyACM0 Finnaly my programm started work and I start testing the board.

temp measurment
I used soldering iron for measuring temperature.

temp measurment results



Switch/button board


I cut the board, soldered the components.


button

The board has 6 pins for connecting fab ISP and 6 pins FTDI.
Started programming process, connected FAB ISP and FTDI cable to the board after to the computer, opened the folder by right click
"open in terminal"
and start typing

• "make -f hello.button.45.make program-usbtiny" "program-usbtiny" specifies the programmer we're going to use change "usbtiny" to "avrisp2" or "dragon_isp" in case of we used that programmers
• typed the command and hit enter "avrdude -p t45 -P usb -c usbtiny -U flash:w:hello.button.45.c.hex"
• installed python's serial module for reading data through FTDI cable "sudo apt install python-serial"
• "ls /dev" and found "ttyUSB0"
• "sudo python term.py /dev/ttyUSB0 9600"

I changed the (du) into (ku)

kuku
button kuku

Code

Magnetic field mesuring board code. I have used neils code to program my board.


		//
		// hello.mag.45.c
		//
		// Hall effect magnetic sensing hello-world
		//    9600 baud FTDI interface
		//
		// Neil Gershenfeld 11/3/13
		// (c) Massachusetts Institute of Technology 2013
		//
		// This work may be reproduced, modified, distributed,
		// performed, and displayed for any purpose. Copyright is
		// retained and must be preserved. The work is provided
		// as is; no warranty is provided, and users accept all 
		// liability.
		//
		
		#include 
		#include 
		
		#define output(directions,pin) (directions |= pin) // set port direction for output
		#define set(port,pin) (port |= pin) // set port pin
		#define clear(port,pin) (port &= (~pin)) // clear port pin
		#define pin_test(pins,pin) (pins & pin) // test for port pin
		#define bit_test(byte,bit) (byte & (1 << bit)) // test for bit set
		#define bit_delay_time 102 // bit delay for 9600 with overhead
		#define bit_delay() _delay_us(bit_delay_time) // RS232 bit delay
		#define half_bit_delay() _delay_us(bit_delay_time/2) // RS232 half bit delay
		#define char_delay() _delay_ms(10) // char delay
		
		#define serial_port PORTB
		#define serial_direction DDRB
		#define serial_pin_out (1 << PB2)
		
		#define nsamples 100 // number of samples to accumulate
		
		void put_char(volatile unsigned char *port, unsigned char pin, char txchar) {
		   //
		   // send character in txchar on port pin
		   //    assumes line driver (inverts bits)
		   //
		   // start bit
		   //
		   clear(*port,pin);
		   bit_delay();
		   //
		   // unrolled loop to write data bits
		   //
		   if bit_test(txchar,0)
			  set(*port,pin);
		   else
			  clear(*port,pin);
		   bit_delay();
		   if bit_test(txchar,1)
			  set(*port,pin);
		   else
			  clear(*port,pin);
		   bit_delay();
		   if bit_test(txchar,2)
			  set(*port,pin);
		   else
			  clear(*port,pin);
		   bit_delay();
		   if bit_test(txchar,3)
			  set(*port,pin);
		   else
			  clear(*port,pin);
		   bit_delay();
		   if bit_test(txchar,4)
			  set(*port,pin);
		   else
			  clear(*port,pin);
		   bit_delay();
		   if bit_test(txchar,5)
			  set(*port,pin);
		   else
			  clear(*port,pin);
		   bit_delay();
		   if bit_test(txchar,6)
			  set(*port,pin);
		   else
			  clear(*port,pin);
		   bit_delay();
		   if bit_test(txchar,7)
			  set(*port,pin);
		   else
			  clear(*port,pin);
		   bit_delay();
		   //
		   // stop bit
		   //
		   set(*port,pin);
		   bit_delay();
		   //
		   // char delay
		   //
		   bit_delay();
		   }
		
		int main(void) {
		   //
		   // main
		   //
		   static uint16_t count;
		   static uint32_t accum;
		   //
		   // set clock divider to /1
		   //
		   CLKPR = (1 << CLKPCE);
		   CLKPR = (0 << CLKPS3) | (0 << CLKPS2) | (0 << CLKPS1) | (0 << CLKPS0);
		   //
		   // initialize output pins
		   //
		   set(serial_port, serial_pin_out);
		   output(serial_direction, serial_pin_out);
		   //
		   // init A/D
		   //
		   ADMUX = (0 << REFS2) | (0 << REFS1) | (0 << REFS0) // Vcc ref
			  | (0 << ADLAR) // right adjust
			  | (0 << MUX3) | (0 << MUX2) | (1 << MUX1) | (0 << MUX0); // ADC4
		   ADCSRA = (1 << ADEN) // enable
			  | (1 << ADPS2) | (1 << ADPS1) | (1 << ADPS0); // prescaler /128
		   //
		   // main loop
		   //
		   while (1) {
			  //
			  // accumulate samples
			  //
			  accum = 0;
			  for (count = 0; count < nsamples; ++count) { 
				 //
				 // initiate conversion
				 //
				 ADCSRA |= (1 << ADSC);
				 //
				 // wait for completion
				 //
				 while (ADCSRA & (1 << ADSC))
					;
				 //
				 // add result
				 //
				 accum += ADC;
				 }
			  //
			  // send framing
			  //
			  put_char(&serial_port, serial_pin_out, 1);
			  char_delay();
			  put_char(&serial_port, serial_pin_out, 2);
			  char_delay();
			  put_char(&serial_port, serial_pin_out, 3);
			  char_delay();
			  put_char(&serial_port, serial_pin_out, 4);
			  char_delay();
			  //
			  // send result
			  //
			  put_char(&serial_port, serial_pin_out, (accum & 255));
			  char_delay();
			  put_char(&serial_port, serial_pin_out, ((accum >> 8) & 255));
			  char_delay();
			  put_char(&serial_port, serial_pin_out, ((accum >> 16) & 255));
			  char_delay();
			  }
		   }	

Temperature mesuring board code


		//
		//
		// hello.temp.45.c
		//
		// thermistor hello-world
		//    9600 baud FTDI interface
		//
		// Neil Gershenfeld
		// 10/27/10
		//
		// (c) Massachusetts Institute of Technology 2010
		// This work may be reproduced, modified, distributed,
		// performed, and displayed for any purpose. Copyright is
		// retained and must be preserved. The work is provided
		// as is; no warranty is provided, and users accept all 
		// liability.
		//
		
		#include 
		#include 
		
		#define output(directions,pin) (directions |= pin) // set port direction for output
		#define set(port,pin) (port |= pin) // set port pin
		#define clear(port,pin) (port &= (~pin)) // clear port pin
		#define pin_test(pins,pin) (pins & pin) // test for port pin
		#define bit_test(byte,bit) (byte & (1 << bit)) // test for bit set
		#define bit_delay_time 102 // bit delay for 9600 with overhead
		#define bit_delay() _delay_us(bit_delay_time) // RS232 bit delay
		#define half_bit_delay() _delay_us(bit_delay_time/2) // RS232 half bit delay
		#define char_delay() _delay_ms(10) // char delay
		
		#define serial_port PORTB
		#define serial_direction DDRB
		#define serial_pin_out (1 << PB2)
		
		void put_char(volatile unsigned char *port, unsigned char pin, char txchar) {
		   //
		   // send character in txchar on port pin
		   //    assumes line driver (inverts bits)
		   //
		   // start bit
		   //
		   clear(*port,pin);
		   bit_delay();
		   //
		   // unrolled loop to write data bits
		   //
		   if bit_test(txchar,0)
			  set(*port,pin);
		   else
			  clear(*port,pin);
		   bit_delay();
		   if bit_test(txchar,1)
			  set(*port,pin);
		   else
			  clear(*port,pin);
		   bit_delay();
		   if bit_test(txchar,2)
			  set(*port,pin);
		   else
			  clear(*port,pin);
		   bit_delay();
		   if bit_test(txchar,3)
			  set(*port,pin);
		   else
			  clear(*port,pin);
		   bit_delay();
		   if bit_test(txchar,4)
			  set(*port,pin);
		   else
			  clear(*port,pin);
		   bit_delay();
		   if bit_test(txchar,5)
			  set(*port,pin);
		   else
			  clear(*port,pin);
		   bit_delay();
		   if bit_test(txchar,6)
			  set(*port,pin);
		   else
			  clear(*port,pin);
		   bit_delay();
		   if bit_test(txchar,7)
			  set(*port,pin);
		   else
			  clear(*port,pin);
		   bit_delay();
		   //
		   // stop bit
		   //
		   set(*port,pin);
		   bit_delay();
		   //
		   // char delay
		   //
		   bit_delay();
		   }
		
		int main(void) {
		   //
		   // main
		   //
		   static char chr;
		   //
		   // set clock divider to /1
		   //
		   CLKPR = (1 << CLKPCE);
		   CLKPR = (0 << CLKPS3) | (0 << CLKPS2) | (0 << CLKPS1) | (0 << CLKPS0);
		   //
		   // initialize output pins
		   //
		   set(serial_port, serial_pin_out);
		   output(serial_direction, serial_pin_out);
		   //
		   // init A/D
		   //
		   ADMUX = (0 << REFS2) | (0 << REFS1) | (0 << REFS0) // VCC ref
			  | (0 << ADLAR) // right adjust
			  | (0 << MUX3) | (1 << MUX2) | (1 << MUX1) | (1 << MUX0); // 20(PB4-PB3)
		   ADCSRA = (1 << ADEN) // enable
			  | (1 << ADPS2) | (1 << ADPS1) | (1 << ADPS0); // prescaler /128
		   ADCSRB = (1 << BIN); // bipolar mode
		   //
		   // main loop
		   //
		   while (1) {
			  //
			  // send framing
			  //
			  put_char(&serial_port, serial_pin_out, 1);
			  char_delay();
			  put_char(&serial_port, serial_pin_out, 2);
			  char_delay();
			  put_char(&serial_port, serial_pin_out, 3);
			  char_delay();
			  put_char(&serial_port, serial_pin_out, 4);
			  char_delay();
			  //
			  // initiate conversion
			  //
			  ADCSRA |= (1 << ADSC);
			  //
			  // wait for completion
			  //
			  while (ADCSRA & (1 << ADSC))
				 ;
			  //
			  // send result
			  //
			  chr = ADCL;
			  put_char(&serial_port, serial_pin_out, chr);
			  char_delay();
			  chr = ADCH;
			  put_char(&serial_port, serial_pin_out, chr);
			  char_delay();
			  }
		   }

Push button board code


		//
		//
		// hello.button.45.c
		//
		// button hello-world
		//    9600 baud FTDI interface
		//
		// Neil Gershenfeld
		// 10/31/10
		//
		// (c) Massachusetts Institute of Technology 2010
		// This work may be reproduced, modified, distributed,
		// performed, and displayed for any purpose. Copyright is
		// retained and must be preserved. The work is provided
		// as is; no warranty is provided, and users accept all 
		// liability.
		//
		
		#include 
		#include 
		
		#define output(directions,pin) (directions |= pin) // set port direction for output
		#define input(directions,pin) (directions &= (~pin)) // set port direction for input
		#define set(port,pin) (port |= pin) // set port pin
		#define clear(port,pin) (port &= (~pin)) // clear port pin
		#define pin_test(pins,pin) (pins & pin) // test for port pin
		#define bit_test(byte,bit) (byte & (1 << bit)) // test for bit set
		#define bit_delay_time 102 // bit delay for 9600 with overhead
		#define bit_delay() _delay_us(bit_delay_time) // RS232 bit delay
		#define half_bit_delay() _delay_us(bit_delay_time/2) // RS232 half bit delay
		
		#define input_port PORTB
		#define input_direction DDRB
		#define input_pin (1 << PB4)
		#define input_pins PINB
		#define serial_port PORTB
		#define serial_direction DDRB
		#define serial_pin_out (1 << PB2)
		
		void put_char(volatile unsigned char *port, unsigned char pin, char txchar) {
		   //
		   // send character in txchar on port pin
		   //    assumes line driver (inverts bits)
		   //
		   // start bit
		   //
		   clear(*port,pin);
		   bit_delay();
		   //
		   // unrolled loop to write data bits
		   //
		   if bit_test(txchar,0)
			  set(*port,pin);
		   else
			  clear(*port,pin);
		   bit_delay();
		   if bit_test(txchar,1)
			  set(*port,pin);
		   else
			  clear(*port,pin);
		   bit_delay();
		   if bit_test(txchar,2)
			  set(*port,pin);
		   else
			  clear(*port,pin);
		   bit_delay();
		   if bit_test(txchar,3)
			  set(*port,pin);
		   else
			  clear(*port,pin);
		   bit_delay();
		   if bit_test(txchar,4)
			  set(*port,pin);
		   else
			  clear(*port,pin);
		   bit_delay();
		   if bit_test(txchar,5)
			  set(*port,pin);
		   else
			  clear(*port,pin);
		   bit_delay();
		   if bit_test(txchar,6)
			  set(*port,pin);
		   else
			  clear(*port,pin);
		   bit_delay();
		   if bit_test(txchar,7)
			  set(*port,pin);
		   else
			  clear(*port,pin);
		   bit_delay();
		   //
		   // stop bit
		   //
		   set(*port,pin);
		   bit_delay();
		   //
		   // char delay
		   //
		   bit_delay();
		   }
		
		int main(void) {
		   //
		   // main
		   //
		   // set clock divider to /1
		   //
		   CLKPR = (1 << CLKPCE);
		   CLKPR = (0 << CLKPS3) | (0 << CLKPS2) | (0 << CLKPS1) | (0 << CLKPS0);
		   //
		   // initialize pins
		   //
		   set(serial_port, serial_pin_out);
		   output(serial_direction, serial_pin_out);
		   set(input_port, input_pin); // turn on pull-up
		   input(input_direction, input_pin);
		   //
		   // main loop
		   //
		   while (1) {
			  //
			  // wait for button down
			  //
			  while (0 != pin_test(input_pins,input_pin))
				 ;
			  put_char(&serial_port, serial_pin_out, 'd');
			  //
			  // wait for button up
			  //
			  while (0 == pin_test(input_pins,input_pin))
				 ;
			  put_char(&serial_port, serial_pin_out, 'u');
			  }
		   }


You can download the folders


Button
Hall sensor
Temperature sensor

Conclusion

I liked this week assignement I was a little ready for it as I learned about sensors, buttons before for my final project control board. And I decided to use for programming computer with UBUNTU.