Skip to content

Week11. Input devices

Group assignment: probe an input device’s analog levels and digital signals

Individual assignment: measure something: add a sensor to a microcontroller board that you have designed and read it

Group assignment can be viewed at this link. - week11 Group assignment

Group assignment:

What I learned this week: 1. The relationship between input devices and microcontrollers; 2. What is an input device? 3. Explanation of professional vocabulary that appears in various component specifications; 4. How to use equipment to measure signals in circuit diagrams

1.The relationship between input devices and microcontrollers

Input devices are devices that convert information from the outside world into signals that a microcontroller can process. These signals can trigger events, change microcontroller state, or provide data input.

Microcontrollers are programmable devices that perform tasks. They use information from the input device to determine what action to take. For example, a microcontroller can read the data from an ultrasonic sensor and display a specific value based on this information, allowing people to understand how far an object is from its current location (this is also my personal project this time). Just like this picture I drew The microcontroller can be programmed to send signals to the ultrasonic sensor and let the sensor feedback the received signal.

Alt text

The relationship between input devices and microcontrollers is crucial because they enable the microcontroller to interact with the outside world. Without an input device, the microcontroller cannot receive any information and cannot perform any useful tasks.

2. What is an input device?

An input device is a device that can feedback external events to a microcontroller through electrical signals. It is like human skin, which can sense temperature, humidity, etc., and transmit these signals to the brain. The input device is the skin and the brain is the microcontroller.Below I list some common input sensors:

  1. light sensor Detect the intensity of light. Used in automatic lighting systems, cameras that adjust exposure, and solar equipment. A lux value or a percentage of maximum light intensity can be provided.

  2. temperature sensor Measure the ambient temperature. Commonly used in climate control systems, industrial processes and electronic equipment to monitor and control temperature. Outputs a voltage or digital signal proportional to temperature.

  3. Humidity Sensor Measure the relative humidity of the environment. Important in weather stations, agricultural applications and indoor climate control. Outputs the humidity value as a percentage.

  4. Accelerometer Measure acceleration along one or more axes. Orientation detection, gaming and vehicle safety systems for mobile devices. Provides a value in g (acceleration due to gravity). Gyro

  5. Measure rotational speed or angular rate. Orientation tracking for navigation systems, drones and virtual reality devices. Outputs data in degrees per second or radians per second.

3. Professional vocabulary

  1. Digital port: These are individual pins that can be set up as inputs or outputs and can handle binary (0 or 1) values.
  2. Analog port: Some microcontrollers have pins that can receive analog voltage levels directly.
  3. Comparators: Compares the input voltages and provides a digital output of both, indicating which input is higher or if they are consistent.
  4. A/D (analog-to-digital converter): Converts an analog input voltage into a numerical value that the microcontroller can process. This is critical for interfacing with sensors that output analog signals.
  5. I2C method: I2C (Inter-Integrated Circuit) is a serial communication protocol that allows multiple devices to be connected to a microcontroller using only two wires (SDA for data and SCL for clock). Input devices such as I2C-enabled sensors or peripheral ICs can send data to the microcontroller following the I2C communication protocol. The microcontroller needs to be programmed to handle I2C communication and extract relevant input data from connected devices.

4. How to use equipment to measure signals in circuit diagrams

In this week’s Group assignment we also need to know about oscilloscopes. I will demonstrate their use below:

  1. Oscilloscope

This is the oscilloscope we have in our laboratory Alt text

Every time you make an oscilloscope measurement, you need to make sure that the measurement probe’s ground wire (usually the black wire or ground clip on the oscilloscope probe) is properly grounded. This is to avoid signal interference or electrical noise, and for safety reasons. Proper grounding can help obtain clearer and more accurate signal waveforms. Alt text

This is the signal after the PCB board I worked on this week was powered on, showing a sine wave

This is the result measured by my classmate using DHT11 sensor.

Alt text

It can be seen from this that the waveforms presented by different input devices will have some differences. Through this study, I learned that the waveform of the circuit signal will vary according to the device. The working principle differs depending on the type of information they are used to convey.

For example, keyboard presses and releases will show such a waveform in an oscilloscope,

Alt text

while an optical mouse will show such a waveform in an oscilloscope.

Alt text

What can waveforms be used for?

Fault diagnosis: By monitoring the current waveform, fault points in the circuit can be found, such as short circuits, open circuits, unstable connections or damaged components.

Analyze circuit performance: An oscilloscope can display the exact time and amplitude information of a signal, helping engineers ensure that the circuit is working correctly as intended.

Design and Debugging: When designing new circuits or modifying existing circuits, oscilloscopes can help engineers test and optimize circuit performance, such as the timing, amplitude, and waveform of signals.

Individual assignment:

This course requires making an input device. I want to use an ultrasonic sensor to test the distance of an obstacle from it and display it in the IDE.

1. Schematic

Here I used XIAO ESP32 C3 and an ultrasonic sensor to build the project. Alt text Alt text

Connect the ultrasonic:

GND to the GND of XIAOESP32 C3

VCC is connected to 3.3V of XIAOESP32 C3

NC (not connected)

SIG connects to GPIO15 of XIAOESP32 C3

then I got the final schematic like this: Alt text

2. PCB design

then I got the PCB board like this

Alt text then Check design rules, perfect! then I can export the gerber file Alt text convert Gerber to PNG Alt text convert PNG to Gcode Alt text

3. Milling the PCB board

The manufacturing steps are explained in detail in week8, so I won’t go into details here. then I got my input board:

Alt text

Here is the kicad file:

week11_input_kicad

Here is the Gcode file:

top_layer_traces_1000dpi (2).png (2).nc

top_layer_outline_1000dpi (2).png (1).nc

4. Programming

As we learned before, this time the program will also consist of two main parts: setup() and loop().

But before running these programs, you need to define some pins and sensor libraries.

  1. Install Ultrosonic sensor library: Alt text

  2. New code need to understand:

const int ultrasonicPin = 2; // 连接超声波传感器的

This is a common routine that uses an Arduino to measure distance and output it through a serial port, often used in robotics and other automation projects. And that’s exactly what I need this input sensor to do.

  1. Another new code need to understand:
#define TRIG_TIME 10 // 触发时间 (微秒)
#define MAX_DISTANCE 200 // 最大距离 (厘米)

Specify TRIGGER_PIN as GPIO2 (pin 2) to simplify the code. Generally, “#define” is used for macro definition, and the above code uses macro definition.

New word: “Macros” are defined using the #define directive followed by the macro name (usually in uppercase letters to distinguish), a space, and text representing the macro value or code. In addition to simple value replacement, macro definitions can also take parameters and be used like functions. Macro definitions have both advantages and disadvantages. The advantages include that it can simplify complex code, but the disadvantages include that it may make it difficult to understand the code and increase the difficulty of debugging.

  1. setup() The pin mode is set to input or output and serial communication is initialized, allowing us to see the measured distance

  2. loop() First need to use digitalWrite() to trigger the ultrasonic emission, then use the pulseIn() function to measure the time from sending to receiving the pulse. This time is then used to calculate the distance corresponding to the round trip time of the echo and output to the serial monitor via Serial.println().

and here is the code

#include <arduino.h>

const int ultrasonicPin = 2; // 连接超声波传感器的

// 引脚定义
#define TRIGGER_PIN GPIO2
#define ECHO_PIN SIG

// 超声波模块引脚
#define TRIG_TIME 10 // 触发时间 (微秒)
#define MAX_DISTANCE 200 // 最大距离 (厘米)

void setup() {
  // 初始化引脚模式
  pinMode(TRIGGER_PIN, OUTPUT);
  pinMode(ECHO_PIN, INPUT);

  // 初始化 Serial 通信
  Serial.begin(9600);
}

void loop() {
  // 触发超声波传感器
  digitalWrite(TRIGGER_PIN, HIGH);
  delayMicroseconds(TRIG_TIME);
  digitalWrite(TRIGGER_PIN, LOW);

  // 等待回波信号
  long duration = pulseIn(ECHO_PIN, HIGH);

  // 计算距离
  float distance = (duration * 0.034) / 2; // 厘米

  // 输出距离
  if (distance < MAX_DISTANCE) {
    Serial.println(distance);
  } else {
    Serial.println("目标超出范围");
  }

  // 等待一段时间再执行下一次测量
  delay(100);
}

Problems encountered during the process:

“Serial.begin(115200)”

This command is used to set the serial port communication rate (baud rate) on the microcontroller, and this is my serial monitor show : Alt text

They are different, so when the distance data appears at the beginning, the output is always in a state of confusion. When I changed “Serial.begin(115200)”, it was fixed


This is the final result after testing

and here is the hero shot Alt text