Assignments
This week we learned about input devices. I had been involved in a weeklong camp and the reviews(exams) had begun so I had to really juggle everything and I was stressed out to some extent too. Long story short, I reminded myself about impermanence(being very philosophical I know) and thats how I survived.
Struggles aside, I was able to explore sensors mostly related to my final project so I am glad that I was able to do it. I explored the dht11 sensor and soil moisture sensor with my very own board. Just as another fun exploration, I tried out the ultrasonic sensor which was LIT too!! I thoroughly enjoyed delving into this world full of sensors and electronics!! I used the microcontroller that I created durign electronics production which you can access here
Table of Contents
- Group Assigment
- Individual Assignment
Measure something: add a sensor to a microcontroller board that you have designed and read it. - Final Project Development
- Tested 2 sensors that I plan on using for my final project
Roadmap

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
For this week's group assignment, we had to test instruments like multimeters and oscilloscopes to observe the signals produced by a sensor.
Learned to visualize electrical signals and waveforms using an oscilloscope.Gained experience in writing and debugging Arduino code for sensor data acquisition.Improved skills in setting up experiments, diagnosing issues, and accurately measuring signals.
Individual Assignment
So, for the solo mission this week, it was all about making my own board sense something. Which is pretty awesome when you think about it – something I built is now interacting with the real world!
Before we upload the code via a USB, we must download certain libraries. For this firstly I had to switch my board to Xiao Esp32 c3 from Arduino Uno and then install dht11 sensor.

First up was the DHT11 sensor, this little guy that tells you how hot and humid it is. The DHT11 is a commonly used Temperature and humidity sensor that comes with a dedicated NTC to measure temperature and an 8-bit microcontroller to output the values of temperature and humidity as serial data.
Image source

This is a code generated by Chatgpt when given the prompt "Write a Xiao esp32 c3 program to read temperature and humidity using a DHT11 sensor. Use the DHT library, define the GPIO pin connected to the sensor, and output the readings to the serial monitor every 2 seconds. "
#include#define DHTPIN 4 // Define the GPIO pin where the DHT11 is connected #define DHTTYPE DHT11 // Define the sensor type DHT dht(4, DHTTYPE); void setup() { Serial.begin(115200); // Start serial communication dht.begin(); // Initialize the DHT sensor } void loop() { float humidity = dht.readHumidity(); float temperature = dht.readTemperature(); // Default is Celsius if (isnan(humidity) || isnan(temperature)) { Serial.println("Failed to read from DHT sensor!"); return; } Serial.print("Humidity: "); Serial.print(humidity); Serial.print("% Temperature: "); Serial.print(temperature); Serial.println("°C"); delay(2000); // Wait 2 seconds before the next reading }
#include <DHT.h>
: Imports the DHT library for easy sensor interaction.#define DHTPIN 4
: Sets pin 4 as the connection for the DHT11 data wire.#define DHTTYPE DHT11
: Specifies that we are using a DHT11 sensor.DHT dht(DHTPIN, DHTTYPE);
: Creates an object named 'dht' to control the DHT11.Serial.begin(115200);
: Starts communication with your computer's serial monitor.dht.begin();
: Initializes the DHT11 sensor.float humidity = dht.readHumidity();
: Reads the humidity and stores it.float temperature = dht.readTemperature();
: Reads the temperature (in Celsius) and stores it.if (isnan(humidity) || isnan(temperature)) { ... }
: Checks if the readings are valid numbers.Serial.print(...)
andSerial.println(...)
: Displays the humidity and temperature on the serial monitor.delay(2000);
: Pauses for 2 seconds before the next reading.
Think of the DHT library as having a special way to ask the DHT11 sensor for its readings. When you use dht.readHumidity()
or dht.readTemperature()
, the library sends a specific request to the sensor. The DHT11 then responds by sending back a stream of numbers representing the humidity and temperature. The library functions understand this stream and convert it into the actual humidity and temperature values that your program can use.
There was a unstable connection between the sensor and the GPIO pin, thus it caused temporary read failures before the connection stabilized.

Ultrasonic sensor
Next, I tried out this ultrasonic sensor, which is basically like giving your board a sense of sight (kind of!). It sends out these super high-pitched sounds that we can't even hear, and then listens for them to bounce back. The HC-SR04 looks a bit like a tiny pair of eyes, one to "shout" the sound and the other to "listen" for the echo.
An ultrasonic sensor is an electronic device that measures the distance of a target object by emitting ultrasonic sound waves, and converts the reflected sound into an electrical signal. Ultrasonic waves travel faster than the speed of audible sound (i.e. the sound that humans can hear).
Image source
How an Ultrasonic Sensor Works (HC-SR04)
An ultrasonic sensor (like the HC-SR04) has two main parts:
- Trigger (TX) – sends out the ultrasonic sound.
- Echo (RX) – receives the sound when it bounces back.
How it works: [This part is fully credited to AI because I asked it to explain the working mechanisms in simple terms.]
- Triggering: You send a short
10-microsecond HIGH
pulse to the trigger pin. This makes the sensor emit an 8-cycle ultrasonic burst at 40 kHz. - Wave travels: The sound wave travels through the air.
- Reflection: If it hits an object, the wave reflects (like an echo) and comes back.
- Echo pin goes HIGH: The echo pin stays HIGH for the time it takes for the echo to return.
- Measure time: Your microcontroller measures how long the echo pin stays HIGH — that’s the round-trip time.
- Calculate distance: Use the formula:
Distance = (Time × Speed of Sound) / 2
In the code that I've used, no external libraries are used. It only relies on built-in Arduino core functions that are available by default in the Arduino IDE.


const int trigPin = 2; const int echoPin = 3; long duration; int distance; void setup() { Serial.begin(115200); // Start the Serial communication at 9600 baud rate pinMode(trigPin, OUTPUT); // Set the trigPin as an output pinMode(echoPin, INPUT); // Set the echoPin as an input } void loop() { // Clear the trigPin by setting it LOW for 2 microseconds digitalWrite(trigPin, LOW); delayMicroseconds(2); // Send a 10µs pulse to trigger the sensor digitalWrite(trigPin, HIGH); delayMicroseconds(10); digitalWrite(trigPin, LOW); // Measure the time it takes for the echo to return (in microseconds) duration = pulseIn(echoPin, HIGH); // Calculate the distance in cm // Speed of sound is 0.034 cm/µs, and we divide by 2 because the pulse travels to the object and back distance = duration * 0.034 / 2; // Debugging: Print the duration for troubleshooting Serial.print("Duration: "); Serial.println(duration); // If the duration is 0, it means the sensor did not receive any echo, so we print a warning if (duration == 0) { Serial.println("No echo received."); } else { // Print the distance to the Serial Monitor Serial.print("Distance: "); Serial.print(distance); Serial.println(" cm"); } delay(500); // Wait for half a second before taking another reading }
const int trigPin = 2;
andconst int echoPin = 3;
: Defines which Arduino pins are connected to the sensor.long duration;
andint distance;
: Declares variables to store the time it takes for sound to return and the calculated distance.Serial.begin(115200);
: Starts serial communication to send data to your computer.pinMode(trigPin, OUTPUT);
: Configures the trigger pin to send signals.pinMode(echoPin, INPUT);
: Configures the echo pin to receive signals.digitalWrite(trigPin, LOW); delayMicroseconds(2);
: Ensures a clean start for the sound pulse.digitalWrite(trigPin, HIGH); delayMicroseconds(10); digitalWrite(trigPin, LOW);
: Sends a short sound pulse from the sensor.duration = pulseIn(echoPin, HIGH);
: Measures how long it takes for the sound to return.distance = duration * 0.034 / 2;
: Calculates the distance to the object.Serial.print(...)
andSerial.println(...);
: Displays the time and distance on the serial monitor.if (duration == 0) { ... } else { ... }
: Checks if a signal was received back.delay(500);
: Waits half a second before taking the next measurement.
The process is like shouting and listening for an echo.
- Shout (Trigger): The line
digitalWrite(trigPin, HIGH); delayMicroseconds(10); digitalWrite(trigPin, LOW);
sends a very short electrical pulse to thetrigPin
. This tells the sensor to emit an ultrasonic sound wave. - Listen for Echo: The
pulseIn(echoPin, HIGH)
line makes the microcontroller wait and measure how long theechoPin
stays HIGH. TheechoPin
goes HIGH when the ultrasonic sound wave bounces off an object and returns to the sensor. The duration of this HIGH signal is the time the sound took to travel to the object and back.
Capacitive soil moisture sensor
Recollection: During the electronics design week, I tested a soil moisture sensor with arduino Uno. This time, I tested the soil moisture sensor with my very own board!! It was pretty cool because it brought me back to when we were first making our own boards! It was like, "Hey, remember me? Let's try this again, but this time with your creation!"
A capacitive soil moisture sensor is a device used to measure the moisture level in soil by detecting changes in the capacitance (electrical charge storage) between two conductive plates inserted into the soil. The principle behind it is based on the fact that water in the soil changes the dielectric constant, and this alters the capacitance between the plates.
They are also more durable. It has 3 pinouts designated for VCC, GND and signal pin. Be careful of the connections!
Image source
I asked chatgpt to give me a code suitable for this project with the prompt "Write an Arduino program to read a soil moisture sensor connected to GPIO pin 2. Display the moisture value and classify it as 'Wet' if the value is below 1800, or 'Normal' if the value is above 1800. The result should be printed to the serial monitor every second." and then I asked it to explain the outputs in the serial monitor.
const int soilMoisturePin = 2; // GPIO 2 void setup() { Serial.begin(115200); delay(1000); pinMode(soilMoisturePin, INPUT); } void loop() { int moistureValue = analogRead(soilMoisturePin); Serial.print("Soil Moisture Value: "); Serial.print(moistureValue); Serial.print(" - Status: "); // Wet or Normal classification if (moistureValue < 1800) { Serial.println("Wet"); } else { Serial.println("Normal"); } delay(1000); }
const int soilMoisturePin = 2;
: Defines the Arduino pin connected to the soil moisture sensor.Serial.begin(115200);
: Starts serial communication.delay(1000);
: Adds a short delay after starting serial communication.pinMode(soilMoisturePin, INPUT);
: Configures the soil moisture sensor pin to read input.int moistureValue = analogRead(soilMoisturePin);
: Reads the moisture level as an analog value.Serial.print(...)
andSerial.println(...);
: Displays the raw analog reading.if (moistureValue < 1800) { ... } else { ... }
: Classifies the moisture level as "Wet" or "Normal".delay(1000);
: Waits one second before the next reading.
This sensor changes its electrical property (capacitance) depending on how much water is in the soil. The analogRead(soilMoisturePin)
function measures the resulting electrical signal (voltage) coming from the sensor. More water in the soil affects the capacitance, which in turn changes the voltage. The analogRead()
function converts this voltage into a digital number. This number represents the moisture level – generally, a lower number indicates more moisture, and a higher number indicates less moisture.

Soil Moisture Sensor Output
Analog Output
If your sensor has an analog output, the values range from 0 to 1023 (on a 10-bit scale for Arduino):
- High values (closer to 1023): Represent dry soil. The higher the value, the less moisture is in the soil, as the sensor detects less capacitance (less water).
- Low values (closer to 0): Represent wet soil. The lower the value, the more moisture is in the soil, as the sensor detects more capacitance (more water).
Digital Output
If your sensor has a digital output, the values will typically be:
- HIGH (1): Represents dry soil, meaning the moisture level is below the threshold set in the sensor. You might need to water the plant.
- LOW (0): Represents wet soil, meaning the moisture level is above the threshold, and the soil has sufficient moisture.
Final Project Development
Honestly, getting the DHT11 and the capacitive soil moisture sensor working with my own board this week felt like a real breakthrough for the Aema Chhim project! It was super cool to see the temperature and humidity readings coming in, and then to actually get a sense of the soil's moisture level. This is exactly what the automated system will rely on, so making sure these sensors are talking to my board correctly is a huge step. It's giving me a much clearer picture of how the whole thing will work in practice, and it's definitely helped me think through the logic I outlined way back in week 05. Seeing it all start to come together like this is pretty exciting!