Skip to content

11. Input devices

2020 Documentation

Introduction

This week, we have to read and use data from the input device. Because of COVID-19, we don’t have access to the lab and the Arduino kits available there.

Experience

In computing, an input device is a piece of computer hardware used to provide data and control signals to an information processing system such as a computer or information appliance. Examples of input devices include keyboards, mice, scanners, digital cameras, joysticks, and microphones. In my case, I have an ultrasonic sensor that measures distance.

The HC-SR04 Ultrasonic Module has four pins: Ground, VCC, Trig, and Echo. The Ground and VCC pins of the module need to be connected to the Ground and 5 volts pins on the Arduino board, respectively, and the Trig and Echo pins to any Digital I/O pin on the Arduino board. Another input device I have is the light sensor. How It Works

The ultrasonic sensor emits ultrasound at 40,000 Hz, which travels through the air. If there is an object or obstacle in its path, the sound will bounce back to the module. By considering the travel time and the speed of sound, you can calculate the distance.

1

Let’s start with the ultrasonic sensor. The sensor can be connected directly to the Arduino board, but I use a breadboard and jumpers to make it look more organized. In the setup, I defined the trigPin as an output and the echoPin as an input. I also started the serial communication to show the results on the serial monitor. After connecting GND and +5V to the Arduino’s GND and VCC, I connected the echo and trig pins to any digital pin; in my case, pins 9 and 10.

Next, I started writing the code by first defining the variables and pins used. Then, I wrote the rest of the code, using tutorials for guidance.

The next step I wanted to take was to use the ultrasonic sensor with an LED. I wanted the LED to light up when the distance is greater than 10 cm. To do this, I added a small section to the code containing an if…else statement.

The if…else statement allows greater control over the flow of code by enabling multiple tests to be grouped. An else clause (if it exists) will be executed if the condition in the if statement is false. The else can be followed by another if test, allowing multiple, mutually exclusive tests to be run. Each test will proceed to the next one until a true condition is encountered. When a true test is found, its associated block of code runs, and the program skips to the line following the entire if/else structure. If no test proves true, the default else block is executed, if present, setting the default behavior.

Note that an else if block may be used with or without a terminating else block, and vice versa. An unlimited number of else if branches are allowed.

1

// defines pins numbers
    const int trigPin = 11;
    const int echoPin = 10;
    // defines variables
    long duration;
    int distance;
    void setup() {
      // initialize digital pin LED_BUILTIN as an output.
      pinMode(LED_BUILTIN, OUTPUT);
    pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output
    pinMode(echoPin, INPUT); // Sets the echoPin as an Input
    Serial.begin(9600); // Starts the serial communication
    }
    void loop() {
    // Clears the trigPin
    digitalWrite(trigPin, LOW);
    delayMicroseconds(2);
    // Sets the trigPin on HIGH state for 10 micro seconds
    digitalWrite(trigPin, HIGH);
    delayMicroseconds(10);
    digitalWrite(trigPin, LOW);
    // Reads the echoPin, returns the sound wave travel time in microseconds
    duration = pulseIn(echoPin, HIGH);
    // Calculating the distance
    distance= duration*0.034/2;
    // Prints the distance on the Serial Monitor
    if (distance <= 20) {
       digitalWrite(LED_BUILTIN, HIGH);
    }
    else {
      digitalWrite(LED_BUILTIN, LOW); 
    }
    Serial.print("Distance: ");
    Serial.println(distance);
    }

After succeeding with the LED task, the next step was to make a DC motor rotate based on the ultrasonic sensor’s data and then make the motor change its rotation direction.

To control the DC motor, a motor shield is needed. Although this might sound complicated, it uses the same pins. I used pins 9 to 12 for this task. Jumpers were only needed for the ultrasonic sensor since the shield connects directly to the Arduino board.

// defines pins numbers
    const int trigPin = 11;
    const int echoPin = 10;
    // defines variables
    long duration;
    int distance;
    void setup() {
      // initialize digital pin LED_BUILTIN as an output.
      pinMode(LED_BUILTIN, OUTPUT);
    pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output
    pinMode(echoPin, INPUT); // Sets the echoPin as an Input
    Serial.begin(9600); // Starts the serial communication
     pinMode(12, OUTPUT); //Initiates Motor Channel A pin
      pinMode(9, OUTPUT); //Initiates Brake Channel A pin
    }
    void loop() {
    // Clears the trigPin
    digitalWrite(trigPin, LOW);
    delayMicroseconds(2);
    // Sets the trigPin on HIGH state for 10 micro seconds
    digitalWrite(trigPin, HIGH);
    delayMicroseconds(10);
    digitalWrite(trigPin, LOW);
    // Reads the echoPin, returns the sound wave travel time in microseconds
    duration = pulseIn(echoPin, HIGH);
    // Calculating the distance
    distance= duration*0.034/2;
    // Prints the distance on the Serial Monitor
    if (distance < 15) {
      delay(1500);
        digitalWrite(12, HIGH); //Establishes forward direction of Channel A
      digitalWrite(9, LOW);   //Disengage the Brake for Channel A
      analogWrite(3, 255);   //Spins the motor on Channel A at full speed

    }
    else if (distance > 16){
     delay(1000);               //Delay 4 seconds Motor rotates in clock wise direction
    digitalWrite(12,LOW);    //Pin#7 as Low
    digitalWrite(9,LOW);    //Pin#8 as Low 
    delay(3000);               //Delay 3 seconds Motor halts
    digitalWrite(9,HIGH);   //Pin#8 as High
    digitalWrite(12,LOW);    //Pin#7 as Low
    analogWrite(3, 255);   //Spins the motor on Channel A at full speed
    //delay(4000);               //Delay for 4 seconds Motor rotates in anti clock wise direction
    }
    Serial.print("Distance: ");
    Serial.println(distance);
  }

Back to the lab

After the lockdown, when our lab reopened, we started working on our final projects. As part of my project, I made an automatic headlight. For the input sensor, I’m going to use an LDR sensor. The board will control the light and use a 9V battery to be autonomous.

Light Dependent Resistor (LDR): Photoresistor

Light dependent resistors (LDRs), or photoresistors, are electronic components used to detect light and change the operation of a circuit based on the light levels.

1

I used KiCad to design my board. Here are the parts I used for the board:

1

sketch of the board;

1

schematic design of the board;

1

1

Solderd bord

1

This board will be used as part of my final project.

2024 Documentation

Group assignment:

Group assignment:Probe an input device(s)’s analog levels and digital signals

This week’s group assignment was led by support instructor Maxim Richard.

This week our group has been exploring analog levels and digital input device signals. We tested the HC-SR04 digital ultrasonic rangefinder sensor and the Sharp GP2Y0A21YK0F analog sensor.

Introduction

This time, I used the board I designed during Electronics Design Week. The plan was to use this board for all electronics weeks, thanks to Adrian’s guidance. The original plan was to make an ohmmeter, but we encountered some challenges along the way.

The principle of a voltage divider is to provide some voltage to a component (in our case, a resistor), measure the voltage drop, and then convert that to resistance using a simple formula. After these manipulations, we can have an ohmmeter.

Here are calculations;

1

Preparing the Arduino IDE.

Befor starting the programing process first I have to set up arduino ide.

Adding the necessary libraries for our microcontroller is easy. You can do this by simply copying and pasting a link, and the library will set up automatically. In my case, it doesn’t work automatically, so I have to add the libraries manually.

1

1

choosing the programmmer; tools > programmmer > Atmel ICE

1

To select the microcontroller family in MegaTinyCore, follow these steps: Open the Tools menu. > Go to Board. >Choose MegaTinyCore. > Find the family with the needed microcontroller.

1

Then select chip, which in our case is the ATtiny1614.

1

Troubleshooting

After following the steps described above and connecting the board to the computer via the Atmel-ICE for programming (using 5V, GND, and UPDI for programming), we also connected an additional 5V for operation, which we sourced from another USB port. To ensure correct connections, I referenced the board drawing from KiCad and the ATtiny1614 pinout. If everything is set up correctly, we expect to see a green light on the Atmel.

1

1

1

But something went wrong: the LED on the board behaved unexpectedly, and we couldn’t load sample programs onto the board. Things started getting hot, and sometimes smoke appeared when we supplied current to the board. We used a multimeter to check the board and found numerous short circuits everywhere. It seemed that all the pins of the chip were connected to each other.

After removing the chip, we identified the problem: the traces were poorly machined and connected together where they shouldn’t have been.

1

We placed board under loupe and carefully removed the copper that was connecting the pins using box cutter knife little by little. After we checked it with multimeter .Than soldered the chip back onto the board. We tried the process again, and this time it started accepting programs.

1

here is the general principe of voltage divider and how it is going to work

Voltage Divider Calculation

A voltage divider can be used to reduce a voltage to a desired level using two resistors connected in series. It also can be used to measure resistance of an unknown resistance. Here’s how we can calculate the output voltage:

Basic Formula:

$$

V_{PA3} = V_{in} \times \frac{R_2}{R_1 + R_2}

$$

By reading the voltage at pin PA3, we can calculate the resistance R1.

1

Programing

In the sample programs, I found a voltmeter program which I used as a base to write a multimeter program. First, I tried it with the serial monitor, measured some voltage, and got correct results. So, we are good to go.

1

After inputting the formula that was sketched on paper and trying again with different resistors, the results are pretty accurate.

1

At this point, I’m in the final stretch. I connected the screen to the board so it could display the resistance. For that purpose, I used Adrian’s documentation since he connected everything in the Fab Lab to that board. First thing was to add libraries for the screen then compile the code and test it with hello world program.

1

1

It was time to connect the two codes together. I took the “Hello World” screen code and copied it into the ohmmeter code, part by part.

#include <LiquidCrystal_PCF8574.h>
#include <Wire.h>

LiquidCrystal_PCF8574 lcd(0x27); // set LCD address 0x27
int count = 0;




// the setup routine runs once when you press reset:
void setup() {
  // initialize serial communication at 9600 bits per second:
  Serial.begin(9600);
     lcd.begin(16,2); // initialize 16x2 LCD
   //lcd.begin(20,4); // initialize 20x4 LCD
   lcd.setBacklight(255); // turn on backlight
   lcd.home(); // go home
   lcd.clear(); // clear display
   lcd.print("Resistance: "); // print text
}
// the loop routine runs over and over again forever:
void loop() {
  // read the input on analog pin pa3:
  int sensorValue = analogRead(10);
  // Convert the analog reading (which goes from 0 - 1023) to a voltage (0 - 3.3V):
  float voltage = sensorValue * (3.3/ 1023.0);

  float resistance = 1000 * (3.3/voltage -1) ;
  // print out the value you read:

  Serial.println(resistance);
   lcd.home(); // go home
   lcd.clear(); // clear display
   lcd.print("Resistance: "); // print text
  lcd.print(resistance);


  delay(1000);


}

First, I checked the resistance with a multimeter on a random board, then measured it with mine. Fortunately, everything worked, and the results were the same.

1

1

Conclusion

First of all, I want to thank our instructors, especially Maxime Richard, for his patience. This week was about real electronics for me, which included not only making the board but also programming it and making it useful for something. There aren’t many things about the 2020 electronics course that I can remember, but this time the knowledge I gained will always stay with me and help me in future weeks and in my own projects.

Sure this was just an introduction for making and programming boards.

Files

KIkad files

VOLKSWAGEN_BEETLE_engravings

VOLKSWAGEN_BEETLE_outline