Esp32 c3 supermini + 4067 Multiplexer troubles
Posted: Tue Nov 12, 2024 8:09 pm
Hello everybody. To introduce myself, i am Marinko, 38y graphic designer, father, and apsolute newbie in fields of electronics. However, lack of time and knowledge did not stop me to try to bring a project to life.
To keep it short, last 6 months i've put every bit of my time to try and build this. I've came to roadblock which i hope will be skipped with some of your help.
Onto the project!
I would love to make grid of 16 force sensitive resistors (4x4) and read their values via Esp32 c3 supermini. This module is my choice due to its
size and it has just enough pins for project including lcd and rotary encoder.
To get 16 sensors to work together (and use limited number of pins) , ive created flex pcb with exposed copper and velostat over it to make a fsrs. All fsrs are conected to a 10k resistors and i have 100uf capacitor to stabilise readings. For software i use Arduino IDE and so far i've managed to stabilise and get pretty nice readings in serial plotter, but all of my reading lines are moving like one - no matter which sensor is pressed. You can imagine how useless that would be.
I will provide simple code that i am using, as well as connection chart (added some resistors on top of it which helped lower the noise, nothing important for main quest.)
Did i do something terribly wrong in planing, code or it is imposible to read 16 fsrs simultaneously? I would appreciate any help. Thank you.
Code:
[Codebox]// Define MUX control pins
#define S0 2 // GPIO 0
#define S1 10 // GPIO 1
#define S2 21 // GPIO 2
#define S3 4 // GPIO 3
// MUX signal pin connected to the ESP32 analog input
#define MUX_SIGNAL_PIN 0 // GPIO 4
// Define the number of MUX channels (4x4 grid, i.e., 16 FSRs)
#define NUM_CHANNELS 16
// Define array to store FSR readings and initial readings
int fsrValues[NUM_CHANNELS];
int initialReadings[NUM_CHANNELS]; // For calibration
// Function to select MUX channel
void selectMuxChannel(int channel) {
digitalWrite(S0, (channel >> 0) & 0x01); // Set S0
digitalWrite(S1, (channel >> 1) & 0x01); // Set S1
digitalWrite(S2, (channel >> 2) & 0x01); // Set S2
digitalWrite(S3, (channel >> 3) & 0x01); // Set S3
delayMicroseconds(10); // Small delay to allow the MUX to switch channels
}
void setup() {
Serial.begin(115200);
// Initialize MUX control pins
pinMode(S0, OUTPUT);
pinMode(S1, OUTPUT);
pinMode(S2, OUTPUT);
pinMode(S3, OUTPUT);
// Initialize the signal pin for analog reading
pinMode(MUX_SIGNAL_PIN, INPUT);
// Record initial readings for each FSR sensor
for (int i = 0; i < NUM_CHANNELS; i++) {
selectMuxChannel(i);
initialReadings = analogRead(MUX_SIGNAL_PIN); // Store initial reading
delay(20); // Allow time for reading
}
}
void loop() {
// Read all FSR values from the MUX and calibrate to start from 0
for (int i = 0; i < NUM_CHANNELS; i++) {
selectMuxChannel(i);
int rawValue = analogRead(MUX_SIGNAL_PIN);
fsrValues = rawValue - initialReadings; // Adjust to start from 0
// Print the calibrated reading for Serial Plotter
Serial.print(fsrValues);
if (i < NUM_CHANNELS - 1) {
Serial.print(","); // Separate values with commas for Serial Plotter
}
}
Serial.println(); // New line for Serial Plotter
delay(100); // Adjust as needed for data refresh rate
}
[/Codebox]
IMAGES:
To keep it short, last 6 months i've put every bit of my time to try and build this. I've came to roadblock which i hope will be skipped with some of your help.
Onto the project!
I would love to make grid of 16 force sensitive resistors (4x4) and read their values via Esp32 c3 supermini. This module is my choice due to its
size and it has just enough pins for project including lcd and rotary encoder.
To get 16 sensors to work together (and use limited number of pins) , ive created flex pcb with exposed copper and velostat over it to make a fsrs. All fsrs are conected to a 10k resistors and i have 100uf capacitor to stabilise readings. For software i use Arduino IDE and so far i've managed to stabilise and get pretty nice readings in serial plotter, but all of my reading lines are moving like one - no matter which sensor is pressed. You can imagine how useless that would be.
I will provide simple code that i am using, as well as connection chart (added some resistors on top of it which helped lower the noise, nothing important for main quest.)
Did i do something terribly wrong in planing, code or it is imposible to read 16 fsrs simultaneously? I would appreciate any help. Thank you.
Code:
[Codebox]// Define MUX control pins
#define S0 2 // GPIO 0
#define S1 10 // GPIO 1
#define S2 21 // GPIO 2
#define S3 4 // GPIO 3
// MUX signal pin connected to the ESP32 analog input
#define MUX_SIGNAL_PIN 0 // GPIO 4
// Define the number of MUX channels (4x4 grid, i.e., 16 FSRs)
#define NUM_CHANNELS 16
// Define array to store FSR readings and initial readings
int fsrValues[NUM_CHANNELS];
int initialReadings[NUM_CHANNELS]; // For calibration
// Function to select MUX channel
void selectMuxChannel(int channel) {
digitalWrite(S0, (channel >> 0) & 0x01); // Set S0
digitalWrite(S1, (channel >> 1) & 0x01); // Set S1
digitalWrite(S2, (channel >> 2) & 0x01); // Set S2
digitalWrite(S3, (channel >> 3) & 0x01); // Set S3
delayMicroseconds(10); // Small delay to allow the MUX to switch channels
}
void setup() {
Serial.begin(115200);
// Initialize MUX control pins
pinMode(S0, OUTPUT);
pinMode(S1, OUTPUT);
pinMode(S2, OUTPUT);
pinMode(S3, OUTPUT);
// Initialize the signal pin for analog reading
pinMode(MUX_SIGNAL_PIN, INPUT);
// Record initial readings for each FSR sensor
for (int i = 0; i < NUM_CHANNELS; i++) {
selectMuxChannel(i);
initialReadings = analogRead(MUX_SIGNAL_PIN); // Store initial reading
delay(20); // Allow time for reading
}
}
void loop() {
// Read all FSR values from the MUX and calibrate to start from 0
for (int i = 0; i < NUM_CHANNELS; i++) {
selectMuxChannel(i);
int rawValue = analogRead(MUX_SIGNAL_PIN);
fsrValues = rawValue - initialReadings; // Adjust to start from 0
// Print the calibrated reading for Serial Plotter
Serial.print(fsrValues);
if (i < NUM_CHANNELS - 1) {
Serial.print(","); // Separate values with commas for Serial Plotter
}
}
Serial.println(); // New line for Serial Plotter
delay(100); // Adjust as needed for data refresh rate
}
[/Codebox]
IMAGES: