Input device
Task:-
Group assignment:
probe an input device's analog levels and digital signals.
Individual assignments:
measure something: add a sensor to a microcontroller board
that you have designed and read it.
Capacitive Touch Sensor
capacitance
From the basics, I have understood what capacitance is, it says that every object in the world stores a tiny amount of electric charge.
In Technical terms
Capacitance(C) is the amount of charge (Q) that can be stored at a given voltage by an electrical component called a capacitor. Unit - Farad(F).
For my project, I need to create a story card system.
As a reference, my instructor showed a product (a learning device) that uses an IR proximity sensor to detect and read cards.
This system works by identifying black and white patterns printed on the card.
Using similar concept, I'm developing story card
To achieve this, designing a custom PCB circuit using an IR sensor, which will detect the patterns on the card when insert it and interpret them accordingly.
IR Proximity Sensor - IR detection circuit
Photo Transistor
photo transistor
it senses light. It works like a switch that is controlled by light instead of a button.
When light falls on it, it turns “on” and allows current to flow.
When there is not much or no light, it stays “off” and very little current flows.
photo transistor
it senses light. It works like a switch that is controlled by light instead of a button.
When light falls on it, it turns “on” and allows current to flow.
When there is not much or no light, it stays “off” and very little current flows.
In an IR sensor system, a phototransistor is the receiver that detects the amount of infrared light sent by the IR LED.
Incase of story card, depending on the surface:
White surface
reflects more IR >> phototransistor recieves more light >> more current flows >> voltage drops at the reading point : lower reading
Black surface
absorbs IR >> photo transistor recieves Less light >> less current flows >> voltage stays higher : higher reading
IR led
#define LED_PIN PIN_PA1
void setup() {
pinMode(LED_PIN, OUTPUT);
}
void loop() {
digitalWrite(LED_PIN, HIGH); // LED ON
delay(1000);
digitalWrite(LED_PIN, LOW); // LED OFF
delay(1000);
}
AI Prompt Used:
I have designed a PCB using the ATtiny1614 microcontroller with an IR sensing setup.
It includes an IR LED connected from GND through a 499 Ω resistor to PA2,
and a phototransistor (PT15-21B-TR8) connected to PA3 with a 1 kΩ resistor to VCC (5V).
There is also a blue power LED with a 499 Ω resistor, a 1 µF capacitor between VCC and GND,
and a debug LED connected from GND through a 499 Ω resistor to PA1.
An FTDI header connected to default pin.
Give program for IR sensor to detect if something hover on top of it and in serial monitor
it has to show “shades” if it detect the object or “sunlight” - if nothing is hovered.
void setup() {
Serial.begin(115200);
}
void loop() {
Serial.println(analogRead(PIN_PA3));
delay(200);
}
#define SENSOR_PIN PIN_PA3
void setup() {
Serial.begin(115200);
}
void loop() {
int sensorValue = analogRead(SENSOR_PIN);
Serial.print("Sensor: ");
Serial.print(sensorValue);
// adjusted threshold
if (sensorValue > 1000) {
Serial.println("SHADE");
} else {
Serial.println("SUNLIGHT");
}
delay(200);
}
// Define the sensor is connected to pin PA3
#define SENSOR_PIN PIN_PA3
void setup() {
// Start serial communication at 115200 baud
// also set the same value in serial monitor
Serial.begin(115200);
}
// Function to get a stable sensor reading
int readAverage() {
int sum = 0; // Variable to store total of readings
// Take 10 readings to reduce noise
for (int i = 0; i < 10; i++) {
// Read sensor and add to sum (like 920+940+.....)
sum += analogRead(SENSOR_PIN);
delay(5);
}
// Return the average value (total / number of readings)
return sum / 10;
}
void loop() {
// Call the function to get a smooth (averaged) sensor value
int sensorValue = readAverage();
// Print label to Serial Monitor
Serial.print("Sensor: ");
// Print the actual sensor value
Serial.print(sensorValue);
// for eg: in serial monitor, it will show "Sensor:923"
// Check if value is above threshold (990)
// Threshold is chosen based on observed values
if (sensorValue > 990) {
// If value is high : object detected, it will print shade.
Serial.println(" shade");
} else {
// If value is low: no object, it wil print sunlight.
Serial.println(" sunlight");
}
// Wait 200 milliseconds before next reading
delay(200);
}
AI Prompt Used:
I have designed a PCB using the ATtiny1614 microcontroller with an IR sensing setup.
It includes an IR LED connected from GND through a 499 Ω resistor to PA2,
and a phototransistor (PT15-21B-TR8) connected to PA3 with a 1 kΩ resistor to VCC (5V).
There is also a blue power LED with a 499 Ω resistor, a 1 µF capacitor between VCC and GND,
and a debug LED connected from GND through a 499 Ω resistor to PA1.
An FTDI header connected to default pin.
My project is a story card system where cards have black and white patterns,
and the IR sensor should detect when a card is inserted and identify these patterns.
I now want to write simple code to read the sensor values and distinguish between
black and white areas on the card.
#define LED_PIN PIN_PA1
void setup() {
pinMode(LED_PIN, OUTPUT);
}
void loop() {
digitalWrite(LED_PIN, HIGH); // LED ON
delay(1000);
digitalWrite(LED_PIN, LOW); // LED OFF
delay(1000);
}
AI Prompt Used:
I have designed a PCB using the ATtiny1614 microcontroller with an IR sensing setup.
It includes an IR LED connected from GND through a 499 Ω resistor to PA2,
and a phototransistor (PT15-21B-TR8) connected to PA3 with a 1 kΩ resistor to VCC (5V).
There is also a blue power LED with a 499 Ω resistor, a 1 µF capacitor between VCC and GND,
and a debug LED connected from GND through a 499 Ω resistor to PA1.
An FTDI header connected to default pin.
Give program for IR sensor to detect if something hover on top of it and in serial monitor
it has to show “shades” if it detect the object or “sunlight” - if nothing is hovered.
void setup() {
Serial.begin(115200);
}
void loop() {
Serial.println(analogRead(PIN_PA3));
delay(200);
}
#define SENSOR_PIN PIN_PA3
void setup() {
Serial.begin(115200);
}
void loop() {
int sensorValue = analogRead(SENSOR_PIN);
Serial.print("Sensor: ");
Serial.print(sensorValue);
// adjusted threshold
if (sensorValue > 1000) {
Serial.println("SHADE");
} else {
Serial.println("SUNLIGHT");
}
delay(200);
}
// Define the sensor is connected to pin PA3
#define SENSOR_PIN PIN_PA3
void setup() {
// Start serial communication at 115200 baud
// also set the same value in serial monitor
Serial.begin(115200);
}
// Function to get a stable sensor reading
int readAverage() {
int sum = 0; // Variable to store total of readings
// Take 10 readings to reduce noise
for (int i = 0; i < 10; i++) {
// Read sensor and add to sum (like 920+940+.....)
sum += analogRead(SENSOR_PIN);
delay(5);
}
// Return the average value (total / number of readings)
return sum / 10;
}
void loop() {
// Call the function to get a smooth (averaged) sensor value
int sensorValue = readAverage();
// Print label to Serial Monitor
Serial.print("Sensor: ");
// Print the actual sensor value
Serial.print(sensorValue);
// for eg: in serial monitor, it will show "Sensor:923"
// Check if value is above threshold (990)
// Threshold is chosen based on observed values
if (sensorValue > 990) {
// If value is high : object detected, it will print shade.
Serial.println(" shade");
} else {
// If value is low: no object, it wil print sunlight.
Serial.println(" sunlight");
}
// Wait 200 milliseconds before next reading
delay(200);
}
AI Prompt Used:
I have designed a PCB using the ATtiny1614 microcontroller with an IR sensing setup.
It includes an IR LED connected from GND through a 499 Ω resistor to PA2,
and a phototransistor (PT15-21B-TR8) connected to PA3 with a 1 kΩ resistor to VCC (5V).
There is also a blue power LED with a 499 Ω resistor, a 1 µF capacitor between VCC and GND,
and a debug LED connected from GND through a 499 Ω resistor to PA1.
An FTDI header connected to default pin.
My project is a story card system where cards have black and white patterns,
and the IR sensor should detect when a card is inserted and identify these patterns.
I now want to write simple code to read the sensor values and distinguish between
black and white areas on the card.
Program to find the threshold values, for no card detecting (air), black surface, white surface.
#define SENSOR_PIN PIN_PA3
#define IR_LED PIN_PA2
void setup() {
Serial.begin(115200);
pinMode(IR_LED, OUTPUT);
digitalWrite(IR_LED, HIGH); // IR LED always ON
Serial.println("=== THRESHOLD TEST START ===");
Serial.println("Place AIR / WHITE / BLACK and observe values");
}
// Averaging for stable reading
int readSensor() {
int sum = 0;
for (int i = 0; i < 30; i++) {
sum += analogRead(SENSOR_PIN);
delay(2);
}
return sum / 30;
}
void loop() {
int value = readSensor();
Serial.print("Sensor Value: ");
Serial.println(value);
delay(300);
}
Sensor Reading Ranges from:
No card(air)
<630
Black surface
<410
white surface
<180
if (value > 550) {
// NO CARD
}
else if (value < 250) {
// WHITE
}
else {
// BLACK
}
#define SENSOR_PIN PIN_PA3
#define IR_LED PIN_PA2
#define DEBUG_LED PIN_PA1
void setup() {
Serial.begin(115200);
pinMode(IR_LED, OUTPUT);
pinMode(DEBUG_LED, OUTPUT);
digitalWrite(IR_LED, HIGH);
}
int readSensor() {
int sum = 0;
for (int i = 0; i < 20; i++) {
sum += analogRead(SENSOR_PIN);
delay(2);
}
return sum / 20;
}
void loop() {
int value = readSensor();
Serial.print("Value: ");
Serial.print(value);
// 🔴 NO CARD (AIR)
if (value > 550) {
Serial.println(" → NO CARD");
digitalWrite(DEBUG_LED, LOW);
}
// ⚪ WHITE
else if (value < 250) {
Serial.println(" → WHITE");
digitalWrite(DEBUG_LED, HIGH);
}
// ⚫ BLACK
else {
Serial.println(" → BLACK");
digitalWrite(DEBUG_LED, LOW);
}
delay(100);
}