Input Devices - Assignment 10

Discovering Input Devices


So the objective of this week is to get introduced to using input devices. Input devices could be anything that send a signal or data to a microcontroller, which could be either digital or analog.

There are many input devices in the market that could be used to gather data and connect the electronics system to the outside world. Those input devices could be a switch, distance sensors (Ultrasonic or Infrared), magnetic sensors, temperature sensor, light sensor, accelerometers and gyro sensors, sound sensors (microphones), vibration sensors, cameras and much more. Technically everything you see in all the equipment and machines you use in your daily life, like the iphone.

In this section, we will learn on how to use and connect a sensor to a microcontroller, and how to use it to collect data from its surrounding.




Assignment

We had a Group Assignment and an Individual Assignment this week.
  1. Group Assignment
  2. Measure the analog levels and digital signals in an input device.
    You can check the Group Assignment on the following link: Group Assignment
  3. Individual Assignment
  4. The individual assignment was mainly to measure something: add a sensor to a microcontroller board that we have designed and read it.


    Machines Used

    The main machines and equipment used in this week's assignment are machines that are usually used to produce any PCB. The list of machines we used are the following:
    1. Roland MDX-40 is the Desktop CNC machine used to mill copper boards.
    2. Tektonix TBS 1052B is the oscilloscope used to observe the operation of the microcontroller circuit board.
    3. National Instruments VirtualBench was used as a digital oscilloscope to observe and document the operation of the microcontroller circuit board.
    4. FLUKE 179 was the multimeter used throughout all the process.
    5. ProsKit SS 207 was the soldering iron used for welding the components.



    Software Used

    The following software were used in this week's assignment:
    1. Eagle
    2. was used to design the PCB.
    3. GIMP
    4. was used to extract and enhance the image of the PCB design.
    5. Fab Module
    6. was the main online platform used to produce the g-code used by the CNC machine.
    7. Arduino IDE
    8. was used to write the code and upload the code.



1- Group Assignment - Measuring the Analog Levels and Digital Signals in an input device



Testing

The assignment this week was to Measure the analog levels and digital signals in an input device.

So our idea was to compare two sensors that would achieve the same job, or solve the same problem. One of them is Digital and the other is Analog.

We choose to use two sensors that measure:
  1. The UltraSonic Sensor (HC-SR04): Digital Sensor
  2. The Infrared Sensor (SHARP 2Y0A21): Analog Sensor
You can check the data sheet for the sensors used from the following links
  • Ultrasonic Ranging Module HC-SR04
  • IR Distance Measuring Sensor Unit
  • Test 1 - Testing the Ultrasonic Sensor

    So the Ultrasonic Sensor is considered to be a Digital sensor because the microcontroller reads a digital signal from it. How the Ultrasonic mainly works by emiting an ultrasound at 40 000 Hz which travels through the air and if there is an object or obstacle on its path It will bounce back to the module. Considering the travel time and the speed of the sound you can calculate the distance.

    The HC-SR04 Ultrasonic Module has 4 pins, Ground, VCC, Trig and Echo. The Ground and the VCC pins of the module needs to be connected to the Ground and the 5 volts pins on the Arduino Board respectively and the trig and echo pins to any Digital I/O pin on the Arduino Board.

    In order to generate the ultrasound you need to set the Trig on a High State for 10 µs. That will send out an 8 cycle sonic burst which will travel at the speed sound and it will be received in the Echo pin. The Echo pin will output the time in microseconds the sound wave traveled.

    Digital Signal Test

    Test 2 - Testing IR Distance Sensor

    The distance sensor produced by Sharp is a popular and relatively low cost solution for measuring distance.

    In this test we will use the 2YOA21 model and the characteristics of this sensor are:

    1. Minimum Measuring Distance = 10cm;
    2. Maximum Measuring Distance = 80cm;
    3. Infrared Proximity Sensor;
    4. Analog output inversely proportional to distance;
    5. Sensor is Ratiometric;
    6. Operating Supply Voltage = 4.5V to 5.5V;
    7. Average Supply Current – Typical = 30mA;
    8. Response Time = 38 ± 10ms.
    The IR distance sensor is connected to a voltage supply, and connected to an analog input to the micro-controller. The microcontroller reads an analog signal, which is technically a value of voltage between 0 and 5 v. Based on the value recieved you can deduce the distance between the sensor and an obstacle.


    Analog Signal Test

    Comparing Both Results

    Concerning the Ultrasonic Distance Measuring sensor, you can easily read the information from it on the Serial Monitor, as the signal recieved from it is a digital signal, which is converted directly by the help of the existing libraries into the measurments and numbers we need.

    However, on the other hand, with the InfraRed Sharp Distance sensor, it's a bit harder to get data from it, casue you need to calibrate it first. The IR sensor sends analog data to the Arduino Board, which is technically a variation is the voltage. Thus what you can read on the serial monitor is a variation in voltage. To convert that into readable information, we should check the values we read with respect to various set distances and calibrate accordingly.

    The video represents how the signal is sent and recieved from and back to the arduino board. The signal recieved at the ECHO signal by the arduino board is a digital signal that represents the variation in distance.
    On the other hand,the picture represents how the signal is received from the IR sensor.It is mainly a voltage level that variates according to the distance.


    2- Individual Assignment - Measure Something



    Hellow Board

    In the individual assignment, we had to measure something using a board we have designed.

    So as a first step, i already built my Hello Board in the past weeks having a built in LDR sensor on it. The board has 2 LEDs of different colors, one Red and one Green, in addition to a Photoresistor (LDR) and a Button. The objective of the board is to measure the light intensity and based on that, the LEDs will turn on. The Green LED will turn ON when there is light and the Red LED will turn on when there is darkness. Note that the frequency of the LEDs flash depends on the intensity of Light or Darkness measured by the LDR. The more the light or darkness, the faster the blink of the LED.

    You can check the full details of the Hello Board by visiting the following link My Hello Board

    Reading Data from the Sensor

    So unfortunately, the Attiny44 chip does not have the capability to connect to the Serial Moniter in the Arduino IDE other than using the software serial, which will occupy more digital inputs and outputs.

    So i chose the threshhold point based on trial and error, by observing the different LED blinking when changing the threshhold point in the software. Thus after dowloading the final code to the new Hello Board, I wanted to test it and check its full capabilities.

    Due to my limitation, i chose to represent the values read was by using the on board LEDs. Thus the board reads an Analog Value from the LDR, and converts it into a frequency for th LEDs blinks.

    So i gave the board an extenal power using an FTDI Basic Breakout - 5V and checked if it works all by itself. And it did work :D. The video represents the functionality of My Hello Board.


    3- Individual Assignment - Building My New PCB



    My Board Design

    As a second step in this week's individual assignment, I wanted to start designing a new board that will be used on board of the Pipe Surveying Robot i am planning to build in my Final Project.

    Thus to do that, some research was done to check the available sensors that would achieve the requirements i need in my system. The main mission of my Robot is to survey existing pipes, gather data related to its path underground, mainly anglural data, and save this data on a memory stick. So the set of sensors (Inputs) that i could be using on my robot are mainly the following:

    1. Ultrasonic Sensor used to measure distance
    2. Infrared Sensor used to measure distance
    3. Accelerometer and Gyroscope used to measure angular acceleration and the angular position
    In addition to that, i had to take into consideration the outputs i needed in my system to include them in the design of my board. I will not go into details this week, but i will just mention the outputs i would be using in my robot. The outputs that i could be using on my robot are mainly the following:
    1. Stepper Motor Drivers used to control the steppr motors that will be installed on my robot.
    2. MicroSd Module used to save data on an external data memory card.


    The next step after chosing the inputs i want to use is to test those sensors. I did my testing using Arduino Uno, because i am planning to use the ATmega328/p as the main microcontroller on my board, which is the same microcontroller used on the Arduino, thus both boards will have almost the same capabilities and limitation.

    1 - Ultrasonic Distance Sensor

    As mentioned above, the Ultrasonic Distance Sensor is a Digital Sensor. The HC-SR04 Ultrasonic Sensor Module has four pins:

    1. Vcc which is connected to the 5V power source, either from the Arduino Board, or any other board
    2. Trigger pin which is connected to a digital pin which will be set as an output pin
    3. ECHO pin which is connected to a digital pin that is set to be an Input
    4. GND which is connected to the GND of the power source or board you are using
    So a Four-Pin connection will be needed on My Board for the Ultrasonic Distance Sensor

    Below is the code used to read data from the Ultrasonic Sensor
    /* * created by Rui Santos, http://randomnerdtutorials.com * * Complete Guide for Ultrasonic Sensor HC-SR04 * Ultrasonic sensor Pins: VCC: +5VDC Trig : Trigger (INPUT) - Pin11 Echo: Echo (OUTPUT) - Pin 12 GND: GND */ int trigPin = 5; //Trig - green Jumper int echoPin = 6; //Echo - yellow Jumper long duration, cm, inches; void setup() { //Serial Port begin Serial.begin (9600); //Define inputs and outputs pinMode(trigPin, OUTPUT); pinMode(echoPin, INPUT); } void loop() { // The sensor is triggered by a HIGH pulse of 10 or more microseconds. // Give a short LOW pulse beforehand to ensure a clean HIGH pulse: digitalWrite(trigPin, LOW); delayMicroseconds(5); digitalWrite(trigPin, HIGH); delayMicroseconds(10); digitalWrite(trigPin, LOW); // Read the signal from the sensor: a HIGH pulse whose // duration is the time (in microseconds) from the sending // of the ping to the reception of its echo off of an object. pinMode(echoPin, INPUT); duration = pulseIn(echoPin, HIGH); // convert the time into a distance cm = (duration/2) / 29.1; inches = (duration/2) / 74; Serial.print(inches); Serial.print("in, "); Serial.print(cm); Serial.print("cm"); Serial.println(); delay(250); }
    You can download the original Arduino Code Used from the following Link

    You can download the data sheet for the Ultrasonic Ranging Module HC-SR04 from the following
  • link
  • Ultrasonic Sensor

    2 - InfraRed Distance Sensor

    The IR sensor is an Analog sensor. The SHARP IR Distance sensor is connected to the control board using 3 pins:

    1. Vcc which is connected to the 5V power supply from the board
    2. GND which is connected to the GND pin on the control board
    3. Vo pin which is the output pin that is connected to an Analog Input pin to the microcontroller
    Saying that, The IR distance sensor requires a 3-pin connector at the control board.

    Below is the code used to read data from the IR Sensor
    int sensorpin = 0; // analog pin used to connect the sharp sensor int val = 0; // variable to store the values from sensor(initially zero) void setup() { Serial.begin(9600); // starts the serial monitor } void loop() { val = analogRead(sensorpin); // reads the value of the sharp sensor Serial.println(val); // prints the value of the sensor to the serial monitor delay(100); // wait for this much time before printing next value }
    You can download the original Arduino Code Used from the following Link

    You can download the data sheet for the SHARP IR Distance Measuring Sensor Unit from the following
  • link

  • InfraRed Sensor

    3 - Accelerometer and Gyro (MPU-6050)

    The main sesnor i will be using in my Pipe Surveying Robot is the the MPU-6050.
    The InvenSense MPU-6050 sensor contains a MEMS accelerometer and a MEMS gyro in a single chip. It is very accurate, as it contains 16-bits analog to digital conversion hardware for each channel. Therefor it captures the x, y, and z channel at the same time. The sensor uses the I2C-bus to interface with the Arduino.
    The MPU-6050 is not expensive, especially given the fact that it combines both an accelerometer and a gyro. Also note that Invensense has combined the MPU-6050 with a magnetometer (compass) in a single chip called MPU-9150.

    The accelerometer measures the acceleration along one direction, while the gyroscope measures the angular acceleration on one axis. The module has 8 pins:

    1. Vcc that could be connected to a 5V or 3.3V power suply. The MPU-6050 module has a voltage regulator on board
    2. GNG pin which is connected to the GND of the control board
    3. SCL which has to be connected to SCL pin of the micro controller, known by the Analog 5 pin on the Arduino Board
    4. SDA pin which has to be connected to the SDA pin of the microcontroller, known by the ANalog 4 pin on the Arduino board.
    5. XDA pin
    6. SCL pin
    7. AD0 pin
    8. INT pin
    The analogic pins are not set on INPUT because it's their default setting. The values read by the analogic pins will be sent to the serial port.

    Open the Serial Monitor, move the sensor and try to see how the values change.

    Below is the code used to read data from the MPU-6050
    #include<Wire.h> const int MPU=0x68; int16_t AcX,AcY,AcZ,Tmp,GyX,GyY,GyZ; void setup(){ Wire.begin(); Wire.beginTransmission(MPU); Wire.write(0x6B); Wire.write(0); Wire.endTransmission(true); Serial.begin(9600); } void loop(){ Wire.beginTransmission(MPU); Wire.write(0x3B); Wire.endTransmission(false); Wire.requestFrom(MPU,12,true); AcX=Wire.read()<<8|Wire.read(); AcY=Wire.read()<<8|Wire.read(); AcZ=Wire.read()<<8|Wire.read(); GyX=Wire.read()<<8|Wire.read(); GyY=Wire.read()<<8|Wire.read(); GyZ=Wire.read()<<8|Wire.read(); Serial.print("Accelerometer: "); Serial.print("X = "); Serial.print(AcX); Serial.print(" | Y = "); Serial.print(AcY); Serial.print(" | Z = "); Serial.println(AcZ); Serial.print("Gyroscope: "); Serial.print("X = "); Serial.print(GyX); Serial.print(" | Y = "); Serial.print(GyY); Serial.print(" | Z = "); Serial.println(GyZ); Serial.println(" "); delay(333); }
    You can download the original Arduino Code Used from the following Link

    You can download the data sheet for the Accelerometer and Gyro (MPU-6050) from the following
  • link
  • MPU-6050

    4 - Preparing the Board Design

    Next i need to design the control board of my pipe surveying robot i am building. Many points had to be taken into consideration while designing the board, mainly the Architecture of the Microcontroller i am using, the pin connection to the sensors and outputs i am using. The different parts of the Control board are described in details below.

    The image represents the different part of the full control board such as:

    1. Microcontroller
    2. Power and Input Pins
    3. Inputs and Output Pins
    4. Power Board


    My Board

    4 a - Microcontroller Connections

    In my first prototype of my board, i will be using the ATmega328/P, which is the same Microcontroller used on the Arduino UNO board.

    Saying that, and based on the data sheet, other than the Analog and Digital pins, the following connections were made:

    1. RESET connections. The Reset pin on the microcontroller will be connected to the Reset Pin of the ISP, and that of the FTDI, plus to a push button to be able to Reset the microcontroller whenever i want.
    2. XTAL1 and XTAL2 pins that will be connected to the external clock, in our case a Crystal having a frequency of 16000 mhz
    3. Debugging LED, that is connected to the SCK (Arduino pin13) which is mainly used for debuggining
    4. Power LED, that is connected in parallel to the input 5V Vcc.
    5. Decoupling Capacitors, that are connected in paraller to the +5V input, to regulate the voltage and current recieved by the microcontroller, and achieve a stable power supply. More details on how to calculate the Decoupling capacitors can be found on this link

    Micro controller Decoupling Capacitors

    4 b - Power and Input Pins

    The main Input Pin headers that i will be using for my board are mainly the following:

    1. Power Pins that will have a Reset Pin, a 3.3V and 5V power source, a GND pin and a Vin pin.
    2. FTDI pins that are used to connect the board to the computer using an FTDI cable
    3. ISP pins that will be used to connect the board to the programmer while programming it.


    Main Pins

    4 c - Inputs and Outputs Pins

    A set of pin headers was prepared for the Inputs and Outputs that will be used in my system and connected tot he Microcontroller i am using.
    Each pin head will have a Vcc pin and a GND pin, plus 1 or 2 dedicated Analog or Digital pins.

    • Inputs
      1. Distance Sensor:I will be using either the Ultrasonic Sensor or the IR sensor. But i need to test both in various settings to decide which better achieves the wanted job. However due to the limitations in the number of pins on my board, one set of pin header was prepared to the distance sensors, that could be used for both the Ultrasonic Sensor or the IR sensor. The pins chosen on the micro-controller are Analog Pins that could be also used as digital inputs or outputs.
      2. Gyro and Accelerometer: The Accerlometer has 8 pins. So i will prepare a pin header that has pins, that will be mainly used to support the sensor flat paraller to the board. However, only Four pins will be connected to the microcontroller, the VCC and GND plus the SCL and SDA pins on the microcontroller.
    • Outputs Outputs pins will mainly control the Motors that move the robot and the MICRO SD card module. More details will be mentioned in the Outputs Week.


    Sensor Pins

    4 d - Power Board

    My board will have one battery as a power source dew to the limtied and tight space. Thus i will need power regulators that will achieve having a 12V, 5 V and 3.3 V power supplies on the board. Saying that a power regulation circuit was added to the board to achieved that.


    Power Board

    5- Production and Testing of My Board

    Due to the time limitations (Easter Break), both the Inputs and Outputs week assignments were grouped in one. So the final design of the Board and its production was done in the Outputs Assignment of next week.
    In the Outputs Week, i finalized the design of the board, produced it, and tested both Inputs and Outputs.
    All details could be found on the following link

    Final Board
    Creative Commons License
    This work is licensed under a Creative Commons Attribution 4.0 International License