Skip to content

11. Input devices

Here is an updated version, I am going to use XIAO and Grove - Ultrasonic Ranger to make a new project for my Input Devices week.

I have one type of XIAO from our lab:
alt text

Getting Started with Seeed Studio XIAO SAMD21

I try reset it, it functions well. alt text alt text

Next, let’s start PCB design!

First, we created a new project.
alt text

To add Kicad library alt text

alt text

alt text

Schemetic Design

XIAO SAMD21:
alt text

Grove-Ultrasonic_Ranger: (Notice that it’s D2 for digital pin in my-designed PCB)

alt text

Adding Components:

alt text

There’s no XIAO SAMD21 in the library,but we can use any XIAO for pcb design as their pin are same.

alt text

Switch to PCB:
alt text

If no components, click here: alt text

The wiring is not looking good to me , so I adjust it base on the order of the pins.

alt text alt text alt text

Go to File- Board Setup- Design Rules- Constraints: alt text

Updated version:
alt text alt text

To add edge cut, first go toFile- Board Setup - Text & Graphics - Defaults: I use 0.5 mm.

alt text

alt text

For inspection: alt text

To fix: Use F.cu layer to add text. alt text alt text

To generate Gerber file:
alt text

Conver Gerber to PNG

To convert Gerber to PNG:
Select all files:
alt text

Select Top- Trace, Top- Cut respectfully to generate PNG, then download zip file on the right head corner.
alt text

To convert PNG to Gcode:

Upload png Trace File and set dpi as 1000.
Set ‘tip diameter’ as 0.4 mm and click ‘send calculated settings’
Click ‘calculate’:
alt text

For the outline file, the parameter needed to be set are as below: Always remember to set the dpi to 1000 use the default parameter is fine. In case broke the tips of V-bits/ tapered bits, slow down the speed to 2mm/s:

alt text

PCB design:

alt text alt text

PCB Milling:

alt text

Soldering and short circuit testing

alt text

alt text

Embedded Programming

Add UltrasonicRanger Library

Step 1. Download the UltrasonicRanger Library from Github.

Step 2. Refer How to install library to install library for Arduino.

Step 3. Copy the code into Arduino IDE and upload. If you do not know how to upload the code, please check how to upload code.

I use ‘add zip library’ to add UltrasonicRanger Library, installed successfully. alt text

I test the example code, and it verified good: alt text

Add Seeeduino to your Arduino IDE

click here to add SAMD21 library.

Add Seeeduino to your Arduino IDE
Click on File > Preference, and fill Additional Boards Manager URLs with the url below:

https://files.seeedstudio.com/arduino/package_seeeduino_boards_index.json

Go to Library search for ‘samd board’, select the one by Seeed Studio.
alt text

I have to turn on VPN to finally installed it. alt text

Select Board and Port

Here, SAMD 21 is not in the list after you install Samd board library. But you can use Seeeduino XIAO board. alt text

Upload the code

I copy this code from wiki page , and change its digital pin to 2, which is by my PCD design : digital isD2.

#include "Ultrasonic.h"

Ultrasonic ultrasonic(2);
void setup()
{
  Serial.begin(9600);
}
void loop()
{
  long RangeInInches;
  long RangeInCentimeters;

  Serial.println("The distance to obstacles in front is: ");
  RangeInInches = ultrasonic.MeasureInInches();
  Serial.print(RangeInInches);//0~157 inches
  Serial.println(" inch");
  delay(250);

  RangeInCentimeters = ultrasonic.MeasureInCentimeters(); // two measurements should keep an interval
  Serial.print(RangeInCentimeters);//0~400cm
  Serial.println(" cm");
  delay(250);
}

Complied successfully!!!

alt text alt text

Heroshot/ Video Demo

Code Explanation

This code is an Arduino program designed to measure the distance to obstacles in front using an ultrasonic sensor and display the results via the serial monitor.

Ultrasonic ultrasonic(2);This line creates an Ultrasonic object connected to digital pin 2. The trigger and echo pins of the ultrasonic sensor are both connected to this pin. The setup() function runs once when the Arduino starts. Here, Serial.begin(9600);initializes serial communication with a baud rate of 9600, which is used to send measurement data to the serial monitor.

Summary:
This code uses an ultrasonic sensor to measure the distance to obstacles in front and outputs the results in both inches and centimeters to the serial monitor. The ultrasonic sensor calculates distance by emitting ultrasonic waves and measuring the time it takes for the echo to return.

Group assignment:

Probe an input device(s)’s analog levels and digital signals Document your work on the group work page and reflect on your individual page what you learned

Click here to group work.

Again , let me show you our devices in Chaihuo :-) alt text

For more tutorial , please check:

In English: How to Use an Oscilloscope

In Chinese: [示波器的使用方法](https://www.bilibili.com/video/BV1Ea41147B1/?spm_id_from=333.880.my_history.page.click&vd_source=de467befc07e98c5fa1b53ff474c2875)

From Group Assignment, I learn this:

Analog Levels

Analog signals are continuous signals that vary smoothly over time. They can take on any value within a certain range and are not restricted to discrete levels. alt text

Example:

Sine Wave: This is a common analog signal used in many applications, such as audio and RF.

Triangle Wave: This type of waveform is often used in signal processing and function generators.

Digital Signals

Digital signals are discrete signals that typically switch between two levels: high (logic 1) and low (logic 0). These signals are used in digital electronics and represent binary data. alt text

Example:

Square Wave: This waveform is typical in digital circuits, representing binary data.

Pulse Wave: Commonly seen in PWM (Pulse Width Modulation) signals used for controlling devices such as motors.

Test my Grove DHT11 and check the digtal signals :

alt text

Individual assignment:

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

To add a temperature & humidity sensor to my-design PCB

I am going to use temperature & humidity sensorGrove DHT11 for this week’s assignment.

First Glance : my Schematic , PCB Design & Pin Headers Matching

Hardware overview - Pinout diagram alt text

Schematic Diagram: alt text

PCB Design:
alt text

In this case, I am going to use DHT 11 Pin Headers. (Though you can also use NeoPixel Headers, too.) Notice, in this way, corresponding Pin see as below:

DHT11 | XIAO
no.4 - D2 ; (you could as call it as A1,GPIO4,4 )
no.2 - VCC;
no.1 - GND;

Embedded Programming

For more preknowledge, review:
my week6
my week8;

Find an Examples :

alt text

Modify Examples :

Modify the code according to your siutation.
alt text

I am using DHT11; SIG pin - D2;

alt text

Select Board & Port

alt text

Upload the code!

alt text

Go to ‘Seriel Monitor’

alt text

HeroShot | Outcome

Success!!!

Code provided :

// Example testing sketch for various DHT humidity/temperature sensors
// Written by ladyada, public domain

// REQUIRES the following Arduino libraries:
// - DHT Sensor Library: https://github.com/adafruit/DHT-sensor-library
// - Adafruit Unified Sensor Lib: https://github.com/adafruit/Adafruit_Sensor

#include "DHT.h"

#define DHTPIN D2     // Digital pin connected to the DHT sensor
// Feather HUZZAH ESP8266 note: use pins 3, 4, 5, 12, 13 or 14 --
// Pin 15 can work but DHT must be disconnected during program upload.

// Uncomment whatever type you're using!
#define DHTTYPE DHT11   // DHT 11
// #define DHTTYPE DHT22   // DHT 22  (AM2302), AM2321
//#define DHTTYPE DHT21   // DHT 21 (AM2301)

// Connect pin 1 (on the left) of the sensor to +5V
// NOTE: If using a board with 3.3V logic like an Arduino Due connect pin 1
// to 3.3V instead of 5V!
// Connect pin 2 of the sensor to whatever your DHTPIN is
// Connect pin 3 (on the right) of the sensor to GROUND (if your sensor has 3 pins)
// Connect pin 4 (on the right) of the sensor to GROUND and leave the pin 3 EMPTY (if your sensor has 4 pins)
// Connect a 10K resistor from pin 2 (data) to pin 1 (power) of the sensor

// Initialize DHT sensor.
// Note that older versions of this library took an optional third parameter to
// tweak the timings for faster processors.  This parameter is no longer needed
// as the current DHT reading algorithm adjusts itself to work on faster procs.
DHT dht(DHTPIN, DHTTYPE);

void setup() {
  Serial.begin(9600);
  Serial.println(F("DHTxx test!"));

  dht.begin();
}

void loop() {
  // Wait a few seconds between measurements.
  delay(2000);

  // Reading temperature or humidity takes about 250 milliseconds!
  // Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
  float h = dht.readHumidity();
  // Read temperature as Celsius (the default)
  float t = dht.readTemperature();
  // Read temperature as Fahrenheit (isFahrenheit = true)
  float f = dht.readTemperature(true);

  // Check if any reads failed and exit early (to try again).
  if (isnan(h) || isnan(t) || isnan(f)) {
    Serial.println(F("Failed to read from DHT sensor!"));
    return;
  }

  // Compute heat index in Fahrenheit (the default)
  float hif = dht.computeHeatIndex(f, h);
  // Compute heat index in Celsius (isFahreheit = false)
  float hic = dht.computeHeatIndex(t, h, false);

  Serial.print(F("Humidity: "));
  Serial.print(h);
  Serial.print(F("%  Temperature: "));
  Serial.print(t);
  Serial.print(F("°C "));
  Serial.print(f);
  Serial.print(F("°F  Heat index: "));
  Serial.print(hic);
  Serial.print(F("°C "));
  Serial.print(hif);
  Serial.println(F("°F"));
}