Skip to content

11. Input devices

I need to admit that I was on a business trip last week when we have learned Input devices. Therefore, I was not able to fully finish the task and create my board. However, I have tried Arduino, to try to figure out what is that. I have tried several input devices like echo-sensor, that allows measuring distance with reflecting ultrasound. I have tied dimmer and temperature sensors. It gave me an idea of what I will do when I come back to Shanghai. After I came back, I have finished the Input and Output weeks for one week. It affected the quality of my work. However, I have the result, and I am happy with that.

So,

The tasks for the last two weeks were:

  • measure something: add a sensor to a microcontroller board that you have designed and read it;
  • add an output device to a microcontroller board you’ve designed, and program it to do something.

Also, I need to say that I decided to make a little bit more complicated boar than we used for our previous assignments. Moreover, Saverio inspired us to do it. Therefore, I have decided to build my clone of Arduino. I have used the information from the previous assignments of earlier students. The tutorial was prepared by Ingrassia Daniele as part of the final project. I have studied what the student already did and tried to improve it. For example, I find out the lack of FTDI connector is not convenient for me.

Also, I need the ISP connector because I like to use the programmer sometimes. Also, I have found that I can make some corrections is the location of capacitors. For example, I put a separate capacitor close to the AREF pin, that provides the reference for the microcontroller regarding the top of the voltage. It is important for the board to have as the reference while using the analog inputs. It helps to make the measures more precise. The link to Satshakit is here

Prototype

Toi figures out if the scheme works I have decided to make a prototype with the breadboard. I have used the AtMega328p-u as it can be easily installed on the breadboard and it has the same features as AtMega328p-au that I will use for my Arduino board.

I could not believe, that with that quality of connections, it worked!

Design

The next step was to create the board in Eagle. As the board has just one layer of traces, it was tricky to design it. I have connected all pins on the microcontroller to external pins to connect different devices. Also, the ISP connector and FTDI connector required some space and made the task to put traces more complicated. Therefore, I need to add a couple zero resistors to jump over some traces. The final result is here.

I need to say in advance that I have made some mistake, that you probably can find on this step.

Mill and solder

For creating G-code, I have used http://fabmodules.org/. The space between traces was so narrow in some places. So need to change the board design several times to get an acceptable result. After the board was milled, I started soldering.

As AtMega328p-au has very tiny legs, it was not very easy to locate these legs on the pads. However, I did it. My Arduino is almost ready on this picture.

Test!

The connection to the computer was not successful. The Arduino program could not recognize the board. I started checking the board and compare it to my initial scheme on the breadboard. I have realized that I missed one trace that connects the button and reset pin on the microcontroller.

I did not have enough time to redesign it. Therefore, I have added the jumper.

It worked!

Add sensor!

The first device that I have connected was echo-sensor.

Program!

I have used standard code for this device. It allows showing the measurements in the serial monitor.

Code

    const int trigPin = 9;
    const int echoPin = 10;

    long duration;
    int distance;
    void setup() {
    pinMode(trigPin, OUTPUT); 
    pinMode(echoPin, INPUT);
    Serial.begin(9600); 
    }
    void loop() {
    digitalWrite(trigPin, LOW);
    delayMicroseconds(2);
    digitalWrite(trigPin, HIGH);
    delayMicroseconds(10);
    digitalWrite(trigPin, LOW);
    duration = pulseIn(echoPin, HIGH);
    distance= duration*0.034/2;
    Serial.print("Distance: ");
    Serial.println(distance);
} 

Test!

I do not have a picture of this. But it worked.

Add output device!

As I was very limited with time, I have decided to use some simple output device as LCD and servomotor. My Idea was to create a device that measures the distance with echo-sensor, shows the result on the monitor and change an angle of the servomotor according to the distance. The whole bunch of devices and wires was looking like this.

Program!

I have combined several functions and first needed to measure the distance with the sensor. Them I should reflect the measures on the monitor. This task was more or less easy. The problem was to come with Idea to relate distance with a certain angle of the servomotor. As the maximum distance that can be shown by the sensor is more than 30 meters.

Also, the echo sensor does not give precise numbers. The measures can be very unstable and jump from several centimeters ever in the situation when nothing is moving in the room. Probably, it happens because the ultrasound travels very randomly in the space and can be reflected from anything. However, my final code is here. It has some issues, but at least it functioned.

#include <Wire.h> 
#include <LiquidCrystal_I2C.h>
#include <Servo.h>

LiquidCrystal_I2C lcd(0x27,20,4);  
Servo myservo;  


int trigPin = 11;   
int echoPin = 12;    
long duration, cm, inches;
int pos = 0;    


void setup() {

  Serial.begin (9600);

  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
  lcd.init();                      
  lcd.init();
  // Print a message to the LCD.
  lcd.backlight();
  lcd.setCursor(1,0);
  lcd.print("Hello, World!");
  lcd.setCursor(1,1);
  lcd.print("Let's measure");
  delay(4000);
  lcd.clear();
  myservo.attach(9);  
}

void loop() {
  digitalWrite(trigPin, LOW);
  delayMicroseconds(5);
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);

  pinMode(echoPin, INPUT);
  duration = pulseIn(echoPin, HIGH);


  cm = (duration/2) / 29.1;     
  inches = (duration/2) / 74;   

  Serial.print(inches);
  Serial.print("in, ");
  Serial.print(cm);
  Serial.print("cm");
  Serial.println();
  lcd.setCursor(1,0);
  lcd.print("Cm");
  lcd.setCursor(4,0);
  lcd.print(cm);
  lcd.setCursor(1,1);
  lcd.print("In"); 
  lcd.setCursor(4,1); 
  lcd.print(inches);
  pos=map(cm,0,300,10,170);
    myservo.write(pos);              
  delay(3000);
  lcd.setCursor(4,0);
  lcd.print("            ");
  lcd.setCursor(4,1); 
  lcd.print("            ");
}

The tricky thing was to refresh the LCD and show new numbers then the distance is changing. I could use command “lcd.clean();” for it. But I decided to blank the needed space on the LCD with “ ” (spaces).

  lcd.setCursor(4,0);
  lcd.print("            ");

Test!

First I have connected the power source to censor and output device directly to the board. But it cannot provide enough level of amperes. Therefore, I have used an external power source.

The video with the test is here. You can see that the LCD shows the measurements and servomotor changes its angle.

Conclusion

Although I have not created something very complicated, I have learned how to build my board, connect different devices to it, combine them and program to get some structure of the mechanism with a certain logic.