Skip to content

11. Input devices

- Probe an input device(s)’s analog and digital signals. Document your work (in a group or individually):

Group assignment page: here.

My individual part:
- I have probed the analog signal of a Joystick.
- I have probed the digital signal of a Sonar sensor.

- Measure something: add a sensor to a microcontroller board that you have designed and read it:

Since for my final project, the only inputs I need are two joysticks and a distance sensor, then I worked on learning how to program an ATtiny microcontroller, using the board that I have made during the Electronics design assignment.

Board used

For this assignment I programmed MyMiniBoard_X14, a board I made during the Electronics design assignment.

Features

ATtinyX14 pinout

For this practice I used the ATtiny1614 from the ATtinyX14 series, but since they have the same pinout, any of them can be used on this board.

AttinyPinOut

Joystick

Component: Grove - Thumb Joystick v1.1

JoystickCircuit

Pin configuration:

Sensor Board ATtiny1614
Vcc 5V Vcc
GND GND GND
x Pin 5 0~
y Pin 4 1~

Code

 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
 /* Made by Jefferson Sandoval during the Embedded programming for the FabAcademy2021.
 *  
 * This is just a simple code in Arduino for testing a Grove - Thumb Joystick v1.1 connected
 * to my own board, MyMiniBoard_X14, based on the ATtiny1614.
 * The code takes the position of the Joystick and converts it to a percentage value.
 * 
 * Documentation: http://fabacademy.org/2021/labs/kamplintfort/students/jefferson-sandoval/assignments/week11/
 */

//Declare variables linked to the pins where the joystick is connected.
const int X = 0;
const int Y = 1;

void setup() {
  Serial.begin(9600); //initialize serial communication
}

void loop() { 

  int ValueX, ValueY; //

  //Read values from Joysticks and convert them to percentages,
  //I used 200 cause the joystick can rotate only 50% on any direction.
  ValueX = map(analogRead(X),0,1023,200,-200); //X axis values
  ValueY = map(analogRead(Y),0,1023,-200,200); //Y axis values

  Serial.print("X: ");
  Serial.print(ValueX);
  Serial.println("%");
  Serial.print("Y: ");
  Serial.print(ValueY);
  Serial.println("%");
  Serial.println(" ");

  delay(100); //Wait 0.1 seconds
}

Performance

Probing the Joystick

I did the test connecting the X pin to Channel A:

Description:

- What we can see on the oscilloscope is the analog signal of one of the potentiometers of the Joystick.

- The negative and positive values that we can see in the signal is what I used in the code to know the direction and percentage of pressure.

Sonar sensor

Component: HC-SR04

Sonar

Pin configuration:

Sensor Board ATtiny1614
Vcc 5V Vcc
GND GND GND
Trig Pin 5 0~
Echo Pin 4 1~

Code

 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
/* Made by Jefferson Sandoval during the Embedded programming for the FabAcademy2021
 *  
 * This is just a simple code in Arduino for testing an HC-SR04 connected to my own 
 * board, MyMiniBoard_X14, based on the ATtiny1614.
 * The code reads the value from the sensor and print it on the Serial monitor.
 * 
 * Documentation: http://fabacademy.org/2021/labs/kamplintfort/students/jefferson-sandoval/assignments/week11/
 */

//Declare variables linked to the pins where the ultrasonic is connected.
const int Trigger = 0;
const int Echo = 1;

void setup() {
  Serial.begin(9600); //initialize serial communication

  //Ultrasonic Sensor setup
  pinMode(Trigger, OUTPUT); //Set Trigger as Output
  pinMode(Echo, INPUT); //Set Trigger as Input
  digitalWrite(Trigger, LOW); //keep Trigger off, it'll be activated during the process (in the loop)
}

void loop() 
{
  long t; //echo width
  long d; //distance in cm

  //Send 10us pulses to the Trigger
  digitalWrite(Trigger, HIGH);
  delayMicroseconds(10); 
  digitalWrite(Trigger, LOW);

  t = pulseIn(Echo, HIGH); //get the pulse width
  d = t/59; //get distance in cm

  //Print distance value on Serial monitor
  Serial.print("Distance: ");
  Serial.print(d);
  Serial.println("cm");

  delay (100); //Wait for 0.1 seconds
}

Performance

Probing the Sonar

Before doing the test I made a quick research cause I didn’t really know what and how I was supposed to probe 😅, then I found a good explanation of the sonar sensors:

SonarGif

Image taken from Last Minute Engineers

In this case I wanted to watch and compare the behavior of Trigger and Echo sensor.

I made the test connecting the Echo pin to Channel A (in yellow), and the Trigger pin to Channel B (in green):

Description:

According to the code uploaded to the board:

- The Trigger sensor produces the 10 µS pulse (the green signal in the oscilloscope) every 100 microseconds.

- The Echo sensor uses the width of the received pulse (the yellow signal in the oscilloscope) to then calculate the distance to the reflected object (my hand in this case).

Files

- JoystickCode.ino
- UltrasonicCode.ino


Last update: June 5, 2021