Week 11. Input Devices¶
In Short¶
This week I tried to make input device to measure water level in the vessel. This can be used in my final project for humidifier device to have a sensor for amount of water. I achieved the result two analog pins for transferring and receiving data on Quentorres board with Seeed XIAO RP2040 and copper sheets.
Group assignment¶
We tried ultrasonic (HC-SR04) and infrared (Sharp GP2Y0A21YK0F) distance sensors. We received values from sensor and for precision we calibrated this values. For that we measures multiple distances with meter and entered real distance to received from sensor values in the google sheets table.
We applied dependency function and it calculated an equation 145712x^-1.05 which we inserted into the code to make values recalculated with equation.
int irsensor=A0;
int sensorValue;
void setup(){
pinMode(irSenor,INPUT);
Serial.begin(9600);
}
void loop(){
sensorValue=analogRead(irSensor);
float distance= 145712*pow(sensorValue,-1.05);
Serial.println(sensorValue);
delay(500);
}
And it converts received values according to function equation. This method showed approximate results with deflection of 3 cm so we tried another way with using NewPing library and tried with another code which was precise.
const int sensorPin = A0;
const long referenceMv = 5000;
void setup() {
Serial.begin(9600);
pinMode(ledPin, OUTPUT);
}
void loop() {
//reading the voltage
int val = analogRead(sensorPin);
int mV = (val * referenceMv) / 1023;
int cm = getDistance(mV);
//display values on the screen
Serial.print(mV);
Serial.print(",");
Serial.println(cm);
delay(1000);
}
//interpolation of distance at 250mV intervals
const int TABLE_ENTRIES = 12;
const int INTERVAL = 250;
static int distance[TABLE_ENTRIES] = {150,140,130,100,60,50,40,35,30,25,20,15};
int getDistance(int mV) {
if (mV > INTERVAL * TABLE_ENTRIES - 1) return distance[TABLE_ENTRIES - 1];
else {
int index = mV / INTERVAL;
float frac = (mV % 250) / (float)INTERVAL;
return distance[index] - ((distance[index] - distance[index + 1]) * frac);
}
}
Individual assignment¶
At first I tried the step response method similar to Adrian’s documentation for AtTiny1614 board. For testing I used same step response board but instead of AtTiny1614 I used RP2040.
I connected accordingly Rx Tx (GPIO 0 and 1 ) pins, power source, gnd of Quentorres board to Adrian’s designed step response board.
Also connected and copper discs.
But for this didn’t work, touch was not recognized. I supposed the reason of not working is that Rx and Tx pins that I connected accordingly the Pinout are not analog pins.
I changed Rx Tx pins from default pinout to 2 Analog pins, (GPIO 28,29) and tried this way.
long result; //variable for the result of the tx_rx measurement.
int analog_pin = 29; // analog pin on Seeed Xiao RP2040
int tx_pin = 28; // analog pin on Seeed Xiao RP2040
void setup() {
pinMode(tx_pin,OUTPUT); //Pin 2 provides the voltage step
Serial.begin(115200);
}
long tx_rx() { //Function to execute rx_tx algorithm and return a value
//that depends on coupling of two electrodes.
//Value returned is a long integer.
int read_high;
int read_low;
int diff;
digitalWrite(tx_pin, HIGH); //Step the voltage high on conductor 1.
read_high = analogRead(analog_pin); //Measure response of conductor 2.
delayMicroseconds(100); //Delay to reach steady state.
digitalWrite(tx_pin, LOW); //Step the voltage to zero on conductor 1.
read_low = analogRead(analog_pin); //Measure response of conductor 2.
diff = read_high - read_low; //desired answer is the difference between high and low.
return diff;
} //End of tx_rx function.
void loop() {
result = tx_rx();
Serial.println(result);
delay(200);
}
This time it was responding to touch. But suddenly I noticed that touch directly on the Quentorres board was recognized, I tried to interact with 28,29 pins directly on the Quentorres board. This worked either so I removed the step-response connector board and connected discs directly to the Quentorress.
Tried to interact with discs using wet sponge to see the impact of water on magnetic field.
I acted according to principle “if it works then do not touch”. And let it like that.
For presentation of this test I turned LED into the process to indicate the result. LED on 26 analog pin writes the result. Also mapped the values.
long result; //variable for the result of the tx_rx measurement.
int analog_pin = 29; //
int tx_pin = 28; //
int LedPin = 26;
void setup() {
pinMode(tx_pin,OUTPUT); //Pin 2 provides the voltage step
pinMode(LedPin,OUTPUT);
Serial.begin(115200);
}
long tx_rx() { //Function to execute rx_tx algorithm and return a value
//that depends on coupling of two electrodes.
//Value returned is a long integer.
int read_high;
int read_low;
int diff;
digitalWrite(tx_pin, HIGH); //Step the voltage high on conductor 1.
read_high = analogRead(analog_pin); //Measure response of conductor 2.
delayMicroseconds(100); //Delay to reach steady state.
digitalWrite(tx_pin, LOW); //Step the voltage to zero on conductor 1.
read_low = analogRead(analog_pin); //Measure response of conductor 2.
diff = read_high - read_low; //desired answer is the difference between high and low.
return diff;
} //End of tx_rx function.
void loop() {
result = tx_rx();
result = map(result, 0, 1023, 0, 255);
if(result<=30){
result = 0;
}
analogWrite(LedPin, result);
Serial.println(result);
delay(200);
}
I continued test with water vessel and another copper sheets. For this I took longer layers to cover the water tank on all height. Sticked two layers on opposite sides of the vessel.
Tried with same connections.
I think this result is what I needed. It worked even simpler way than I expected. There is only two analog pins in role of Tx and Rx and 1k resistor on Rx.
Conclusion¶
This week I tried to make some input sensors that can be used in the final project. I discovered how to make microcontroller interact with inputs and outputs, I leaned about sensor devices and tried few of them and I tried to go deeper in programming, and interacted with serial monitor, read, write commands, etc. A lot of new for me.