Skip to content

11. Input Devices

This weeks task was to connect and test an INPUT device with my the microcontroller board that I designed and built in previous assignments.

Group work

  • This weeks group assignment was to probe an input device’s analog levels and digital signals. Our groups work was documented here: Input devices- Group assignment

Individual assignment

Input Device

  • I decided to go with an ultrasonic sensor as my input device as this is the sensor I intend to use in my final project.

  • In my final project it is intended to be used to simply detect when a robot passes the finish line in a maze.

  • When I was looking at possible ultrasonic sensors I came across a few that needed to be connected to MOSI/MISO ports on a microcontroller. Although most did not need these ports specifically. Just to accomodate either type, I decided to design my board’s input device connector to be connected to these MOSI/MISO ports on the microcontroller ATTINY1614.

The attiny1614 layout: attiny1614

  • The ultrasonic sensor I used was the type most common in Arduino accessory kits: AHC-SR04 Ultrasonic Sensor. ultrasonic sensor
  • It uses an ultrasonic signal to measure distance by emmiting a high frequency sound and measuring how long it takes for that sound to return. Kind of like sonar.
  • It can measure a range of between 2cm and 400cm.
  • It can operate with an input voltage between 3V and 5V.

Connecting Hardware

  • The AHC-SR04 ultrasonic sensor requires 4 connected pins. Voltage (Vcc), Trigger, Echo, and Ground (GND).
  • I designed my board with the input connector in this same order. From top to botttom:

  • Vcc

  • Miso/ PIN 12
  • Mosi/ PIN 11
  • GND

sensor port

  • I could have plugged the ultrasonic sensor directly into the female pins on my board like I did with the OLED screen in output devices assignment, but i decided to connect it with M/F jumper wires instead so I can freely move the sensor around.

  • I then connected the UPDI/FTDI converter to the boards UPDI port, and the FTDI cable into the converter’s other end. Then the FTDI cable into the computer’s USB port.

  • I left the OLED screen connected the same way it was in output device assignment since I am going to be using both together. connected updi.

Programming

This part was already done in the Output devices week, but I’ll just show the steps here again:

Setting up the programming environment:

  • First I had to install the Arduino IDE software.

  • I followed the same procedure as in Quentores Xiao RP2040 project page, but with the following changes:

  • I had to add the “additional board manager URL” to find the necessary libraries. The one for the Attiny was “http://drazzy.com/package_drazzy.com_index.json”. ide add url

  • In the “board manager” I had to install the “MegaTinyCore by Spence Konde”. ide add megatiny

  • I connected my FTDI cable to my USB port and my UPDI connector. And My UPDI cable to the UPDI port on my board to get a connection.

  • I selected the attiny1614 from the tools menu ide select board

  • I selected the programmer as “SerialUPDI SLOW” ide programmer

Working Program

I used the code for the ultrasonic sensor with OLED screen that I created in the Wowki simulator ultrasonic OLED example in my embedded programming assignment MY CODE as a starting base and made small modifications.

My code waits for the button to be pressed first and then constantly measures the distance to the nearest object infront of the ultrasonic sensor and outputs it to the OLED screen and the serial monitor. It also lights the LED when the distance is less than 100.

I needed to make these modifications to assign the proper pins that I was using on my board since they were different from the in the simulation. changes:

  • line 23: buttonPin = 10
  • line 24: ledPin = 0
  • line 28: ECHO_PIN 9
  • line 29: TRIG_PIN 8
  • line 83: setTextSize(2) [the display was a little too small in size 1]

My Code:

/*
James Khan Fab Academy INPUT DEVICE week.
https://fabacademy.org/2021/labs/vancouver/students/james-khan/assignments/week06/

started by using these Wowki samples:
https://wokwi.com/projects/375237011181407233
https://wokwi.com/projects/290056311044833800
https://wokwi.com/projects/365718993906978817
*/

#include <Wire.h>             // for OLED
#include <Adafruit_GFX.h>     // for OLED
#include <Adafruit_SSD1306.h> // for OLED

const int SCREEN_WIDTH = 128; // OLED display width, in pixels
const int SCREEN_HEIGHT = 64; // OLED display height, in pixels

// Declaration for an SSD1306 display connected to I2C (SDA, SCL pins)
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1); // for OLED


// Define variables button and LED
int buttonPin = 10; // Digital pin where the button is connected.
int ledPin = 0;   // Digital pin where the LED is connected.
bool buttonState = false;

// DEfine variables Ultrasonic
#define ECHO_PIN 9
#define TRIG_PIN 8

void setup() {
  pinMode(ledPin, OUTPUT);    // LED pin as an output.
  pinMode(buttonPin, INPUT);  // Button pin as an input.

    // for OLED

  Serial.begin(9600);

  if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) { // Address 0x3D for 128x64
    Serial.println(F("SSD1306 allocation failed"));
    while(true);
  }

    // for ultrasonic
  // Serial.begin(115200);    this was the ultrasonic samples code that conflicted
  pinMode(LED_BUILTIN, OUTPUT);
  pinMode(TRIG_PIN, OUTPUT);
  pinMode(ECHO_PIN, INPUT);

}


float readDistanceCM() {
  digitalWrite(TRIG_PIN, LOW);
  delayMicroseconds(2);
  digitalWrite(TRIG_PIN, HIGH);
  delayMicroseconds(10);
  digitalWrite(TRIG_PIN, LOW);
  int duration = pulseIn(ECHO_PIN, HIGH);
  return duration * 0.034 / 2;
}

void loop() {
      // read state of button only if buttonstate is LOW
  if (buttonState == LOW){
    buttonState = digitalRead(buttonPin);
  }

  if (buttonState == HIGH){ // once buttonState is HIGH then run rest of code

  float distance = readDistanceCM();    // ultrasonic read 
  if (distance < 100){                  // LED on or off?
    digitalWrite(ledPin, HIGH);
  } else {
    digitalWrite(ledPin, LOW);
  }

  Serial.print("Measured distance: ");  // serial output static 
  Serial.println(distance);     // serial output distance
  delay(100);

  display.clearDisplay();               // oled clear
  display.setTextSize(2);               // oled setup
  display.setTextColor(WHITE);
  display.setCursor(0, 10);
  display.println("Distance:");         // static display
  display.println(distance);    // display distance
  display.display();
  delay(100);

  }

}

I then clicked the “upload” button in the Arduino IDE software.

After a long list of updates I in the output window, I received the message that the upload was completed successfully.

In order for the serial monitor data to be recieved I had to connect the FTDI cable to the FTDI port on my board. So I disconnected the FTDI cable from the UPDI converter and connectted it to the FTDI port on the other side of my board.

ftdi connected

Here is my programme working on my board:

NOTE: When the program starts, the OLED displays the last output that it displayed. This is why, even though I hadn’t pressed the button yet to start recording the disptance, it was already displaying a measurement.


Last update: April 15, 2025