uncategorized

Week 11- Input Devices

This week is about getting familiar with a range of different sensors and how to make use of them. Sensors are electronic devices used to sense/measure quantities like heat, pressure, movement, speed, etc. You can see that some of these attributes are correlated like distance and speed are related which means that the same sensor could be used to measure both. So these are the things to be explored this week.

Bouncing and debouncing

Bouncing is a phenomenon seen with mechanical switches. You will observe that when a button is pressed and that change is state is not all f a sudden but a gradual ones starting with random states and finishing in a stable one. Mechanical switches when pressed/switch will rebound a few times before resting into a state. The process off eliminating bouncing is called debouncing. Their are two ways of debouncing

  • Hardware debouncing: where things like logic circuits are used to nullify bouncing or using simple passive circuits like RC circuit.
  • software debouncing: where software algorithms are used to detect and overcome the bounces. These can become complex when there are multiple switches.

A bit about the sensors

Photo transistor

It is essentially a semiconductor transistor with a transparent cover. It gives better sensitivity than photodiode. It provides better gain and is cheap making it quiet popular. The transistor operates in the active region and usually base in left disconnected. The exposure to light at the base area due to the transparent sheild causes base current and thus the biasing.

Thermistor

The name itself gives good idea about the device. It is a thermally sensitive resistor, it changes the resistance according to the surrounding temperature. There are of two kinds

  • NTC: Negative temperature coefficient, the resistance will reduce with increase in temperature.
  • PTC: Positive temperature coefficient, the resistance increases with increase in temperature.

This could be used as a branch of an electronic bridge so as to measure the changes.

Neils Examples

Neil has listed out quiet a few number of sensors as part of this weeks study material. He went through it one by one in his lecture. I have worked with some of the sensor like the sound, heat etc. I always liked to play with visible light so decided to work on a light sensor.

Light Sensor

For this I considered Neils light sensing boards. Keeping the original board as reference I drew a new circuit in Eagle and milled using modella and soldered in the lab itself. Eagle designAfter all that I when I tried to program it it showed multiple errors.

Trouble shooting

  • First of all I checked whether the cables where connected correctly.
  • Then I cross checked the design I made and found out that I missed to connect reset pins of the ISP programmer and ATtiny45. So I had to connect a jumper to solve it !. When tried to program after this everything worked fine.

I had a green LED as light source which turned out to be not bright enough. Planning to swap it for red/orange LED.

Programming

Their is Neil’s code available for testing out. I downloaded the C-code and then there is a visual interface for showing the detection of light. The interface is done in python which uses PySerail for serial communication to the board via FTDI. You can get the code here.
It was a bit confusing at the first time as I had some issues as my default Python version is 3 and the code is written for 2. Then there was the issue of arguments, where we are supposed to give the path to the USB we are using as argument to the code while executing which I figured out after reading through the code. So after all this it finally worked.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
//
//
// hello.reflect.45.c
//
// light reflection synchronous detection hello-world
// 9600 baud FTDI interface
//
// Neil Gershenfeld
// 10/25/12
//
// (c) Massachusetts Institute of Technology 2012
// 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 <avr/io.h>
#include <util/delay.h>

#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 led_port PORTB
#define led_direction DDRB
#define led_pin (1 << PB3)

#define nloop 100 // number of loops 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 unsigned char count;
static uint16_t on,off;
//
// 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);
set(led_port, led_pin);
output(led_direction, led_pin);
//
// init A/D
//
ADMUX = (0 << REFS2) | (0 << REFS1) | (0 << REFS0) // Vcc ref
| (0 << ADLAR) // right adjust
| (0 << MUX3) | (0 << MUX2) | (1 << MUX1) | (0 << MUX0); // ADC2
ADCSRA = (1 << ADEN) // enable
| (1 << ADPS2) | (1 << ADPS1) | (1 << ADPS0); // prescaler /128
//
// main loop
//
while (1) {
//
// accumulate
//
on = 0;
off = 0;
for (count = 0; count < nloop; ++count) {
//
// LED off
//
set(led_port, led_pin);
//
// initiate conversion
//
ADCSRA |= (1 << ADSC);
//
// wait for completion
//
while (ADCSRA & (1 << ADSC))
;
//
// save result
//
off += ADC;
//
// LED on
//
clear(led_port, led_pin);
//
// initiate conversion
//
ADCSRA |= (1 << ADSC);
//
// wait for completion
//
while (ADCSRA & (1 << ADSC))
;
//
// save result
//
on += 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, (on & 255));
char_delay();
put_char(&serial_port, serial_pin_out, ((on >> 8) & 255));
char_delay();
put_char(&serial_port, serial_pin_out, (off & 255));
char_delay();
put_char(&serial_port, serial_pin_out, ((off >> 8) & 255));
char_delay();
}
}
The files of light sensor - light_sensor

finished board

Temperature Sensor

Moving further on, in week 13 I decided to combine an input and output device to make something. I planned to make a wrist watch/bracelet which can detect and sound an alarm during hypothermia. Please take a look into my Output Devices documentation for details.

Share