13. Input Devices

make the board out using Eagle

I used the distance board in shcedule as a reference

I added a Attiny45, a electric capacity, a resistance, a 2*3 jack, a 1*6 jack, a 1*4 jack and a distance mode in my board.

Here you see is what I design:

I designed two version of my board and both of them are really tiny! ( but it can be cutted out )

Get the first version files!

Get the second version - tiny version files!

cut the board out and solid the components

As usual, I used the Fabmodule to calculate and used SRM-20 machine to cut it out

Since it is really tiny of my board, it just took me less than 2 minutes to cut it out!

Then I solid the components to the board. It did not take me long time since I was skilled in it.

But the first time I failed to get the distance mode to the board - so I remade a board and success

program the board and test the board

I used the program files in the schedule to program my board

Get the code files.

And I used the FabISP as ISP ( since I couldn not find my fabisp out, I remade a FabISP. )

used the tutorial of how to program the board by using the c files

(I found that it should use the usbtiny to program the board as the code when using FabISP as ISP)
open the terminal on the computer and code:
make -f hello.HC-SR04.make
then code:
sudo make -f hello.HC-SR04.make program-usbtiny
                            

And here I got:

Then link the board to and then the computer.

I tried several times : I remade the board and tried to use other methods like using arduino uno as the serial port

But I failed and here I got:

I have already tried several Baudrate like:9600, 57600, 115200 ...

Then I tried another input board: the light

Here is my design:

Get it.

Get the code files(schedules' files)

Then I tested it again and failed

Then I think it may need the py file from schedule to get the serial information.

So I used the files from the schedule and code in the terminal:


python hello.light.45.py /dev/tty.usbserial-A50285BI

But I got:

Traceback (most recent call last):
  File "hello.light.45.py", line 16, in <module>
    import serial
ImportError: No module named serial

                            

I search it in the google and I code:


pip install pyserial
                            

After doing that, I retried and failed again.

I still can't work it out, maybe there are some problems with my serial ports -- my instructor used it to test the board he designed last year, which was working at that time, and met the same problems as I did.

fix the problem

After reading the Attiny44 data sheet, I found: the trig pin to Pin3 in arduino and echo pin to Pin4 in arduino

Tried so many times - took me 2 weeks, I finally used the Arduino code:

                            
#include <SoftwareSerial.h>
SoftwareSerial mySerial (0,2);
int trig = 3;
int echo = 4;
long duration, distance ;
void setup() {
mySerial.begin(9600);
pinMode(trig, OUTPUT);
pinMode(echo, INPUT);
}
void loop() {
digitalWrite(trig, LOW);
delayMicroseconds(2);
digitalWrite(trig, HIGH);
delayMicroseconds(10);
digitalWrite(trig, LOW);
duration = pulseIn(echo,HIGH);
distance = duration / 58;
mySerial.print("The Distance is: ");
mySerial.println(distance);
delay(100);
}
                            

And I connect my board with TTL to the arduino with 9600 bound and success:

After knowing how the arduino code work:

First, DigitalWrite trig to HIGH for x time and then DigitalWrite it to LOW
Then, Analize the Echo ( time )
Last, use the formula s = vt*0.5 to calculate it
                            

And here is what I then code:


#include <SoftwareSerial.h>
SoftwareSerial mySerial (0,2);
long distance;
void setup() {
mySerial.begin(9600);
pinMode(3, OUTPUT);
pinMode(4, INPUT);
}
void loop() {
digitalWrite(3, LOW);
digitalWrite(3, HIGH);
delayMicroseconds(100);
digitalWrite(3, LOW);
distance = 0.343 * 0.5 * pulseIn(4,HIGH);
// s=vt (s = 1/2 * total s)
mySerial.println(distance);
mySerial.print("cm");
delay(1000);
}