Virtual analog ports

From MDD wiki
Revision as of 11:13, 26 March 2021 by Xiduzo (talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

Virtual analogue ports

Create virtual analogue ports for multiple analogue sensor outputs into a single physical analogue input on your microcontroller.

If you don't have enough analogue ports on your microcontroller to read all sensor data we can utilize some unused digital pins to create virtualized analogue pins.

Scenario

In this document we will be using a Wemos D1 mini pro. The same principles apply to any micro-controller.

Read multiple analogue sensors with just a single analogue input.

Setup the code

First, we need to set up our pins. In the example given we use D1 and D5 as our sensor voltage pins and A0 for our analogue input pin

const int amountOfSensors = 2;
const int sensors[amountOfSensors] = { D1, D5 };

const int input = A0;

Next, we will set up all the pins to make sure we have our input/outputs correct

void setup() {
  // Set all sensors as output
  // We want to control if we OUTPUT 5v to the sensor or not
  for (byte i = 0; i < amountOfSensors; i++) {
    pinMode(sensors[i], OUTPUT);
  }

  // Make sure we have and analog INPUT
  pinMode(input, INPUT);

  // Begin the serial so we can read from the serial monitor
  Serial.begin(9600);
}

In order to validate if we get the correct output, we can define a PrintInput method

void PrintInput() {
  Serial.print("Value: ");
  Serial.println(analogRead(input));
}

In order to switch the sensor, we would like to read from we can define a ToggleInput method

void ToggleInput(int newActiveSensor) {
  // Turn off all sensors
  for (byte i = 0; i < amountOfSensors; i++) {
   digitalWrite(sensors[i], LOW);
  }

  // Turn on the requested sensor
  digitalWrite(newActiveSensor, HIGH);

  // Wait 100ms to make sure the sensor is active
  delay(100);

  Serial.print("Activated Sensor: ");
  Serial.print(newActiveSensor + ". ");
}

To put it all together we can define our main Loop method

void loop() {
  // Turn on sensor 1 and print the output
  ToggleInput(sensors[0]);
  PrintInput();

  // Turn on sensor 2 and print the output
  ToggleInput(sensors[1]);
  PrintInput();

  delay(1000 * 2);
}

Electrical schema

Make sure the white rings on the capacitor are facing away from the sensor and towards the micro-controller.

schema

The locations of the digital and analogue pins may differ from micro-controller to micro-controller.

Components

  1. 2x capacitor
  2. 2x analog sensor
  3. 1x micro-controller with at least 1 analog input
  4. 10x male-male jumper cables
  5. 1x breadboard
  6. 1x micro-controller
  7. 1x usb data cable


Connect everything

Below you will see a protoboard where we have connected all the necessary wires to the Arduino.

protoboard

In order to connect the sensors we need to connect the Sensor 1 GND to the ground, the Sensor 1 5V (D1) to the power and the Sensor 1 signal to the signal output of the first sensor. Repeat the process for sensor 2.

Running the program

After uploading all the code (in a single file) to the Arduino and connecting both sensors we should see a similar output given by the Arduino in the Serial monitor.


output