2 analog output to 1 analog input

Hi there,

So I have a 2 axis Joystick module that I want to connect to a NodeMCU.
The problem is the Joystick has 2 analog outputs and the NodeMCU only has 1 analog input.
I found this tutorial here and I followed all the steps, but wrote the code in Arduino IDE and IT DIDN'T WORK...

So the question remains, is there a way to connect a 2 axis Joystick to NodeMCU?

Here's the code:

#define xPin D1
#define yPin D2
#define aPin A0
uint8_t xAxisValue = 0;
uint8_t yAxisValue = 0;
void setup() {
  Serial.begin(9600);
  pinMode(xPin, OUTPUT);
  pinMode(yPin, OUTPUT);
  digitalWrite(xPin, LOW);
  digitalWrite(yPin, LOW);
}

void loop() {
  digitalWrite(xPin, HIGH);
  digitalWrite(yPin, LOW);
  xAxisValue = analogRead(aPin);
  delay(100);
  digitalWrite(xPin, LOW);
  digitalWrite(yPin, HIGH);
  yAxisValue = analogRead(aPin);

  Serial.print("x: "); Serial.print(xAxisValue);
  Serial.print("    y: "); Serial.println(yAxisValue);
  delay(400);
}

And here is the circuit diagram:

That is a poor description of the problem. It gives us no real information with which to work.

What is the part number of the transistors?

Compare your wiring with these popular NPN transistor pinouts.
2n3904 pin out
2n2222 pin out

1 Like

I'm so sorry I forgot to tell what really was the problem, so the values range from like 0 to 150, and are not really working well.

I drag the Joystick in the position:
UP: X is 0 Y is 134
DOWN: X is 15 Y is 144
LEFT: X is 0 Y is 134
RIGHT: X is 14 Y is 144

Also the transistors I use are TP31C transistors, NPN type.
And I know in the picture is an Arduino Uno, but I use it for testing purposes, I already tried the same configuration on NodeMCU with almost the same result, the values were a bit different.

The circuit diagram:

Poor choice, this is a power transistor and they have very low gain. The configuration is an emitter follower so we are not talking about voltage gain but current gain. It well could explain your poor results. I would use a small signal transistor instead.

1 Like

I would use an analog multiplexer, like this one

In the orientation you show there, 2N3904 is E-B-C.

1 Like

I will try and change the transistors then. I went loco trying to understand just why it wouldnt work.
Thanks a lot mate!

So you're saying in the circuit diagram is used a E-B-C transistor and I used a B-C-E transistor and that's why?

@obi_paws_kenobi
I clicked the Reply to groundFungus. His depiction of the device is inaccurate.

GrumpyMike noted the cause of your trouble (TIP31).

Do you have a voltmeter?

1 Like

I do have a voltmeter, but, I checked again and I'm not sure, but in the circuit diagram the Emitter of the Transistors are wired to the Analog Pin, while I wired the Analog Pin to the Base of my TIP31C Transistor..

Then reconfigure that.

1 Like

Is that line that says analog out actually the analog in to the Node?
If so do you not need a pull down resistor?

Also you don’t need the base resistors because you are using the transistor in the emitter follower mode.

1 Like

Sir yes sir!

Well I reconfigured the wires and still, it's not working.
Turns out I declared the analog value variables as 8bit integers...

But after rewriting the code, i get the next values:
UP: X is 12 ; Y is 510.
DOWN: X is 682 ; Y is 603.
LEFT: X is 570 ; Y is 22.
RIGHT X is 572 ; Y is 684.

Guess the right choice right now is to get different transistors, or just get an analog multiplexer.

Why not use two AnalogInputs ?

You mentioned a NodeMCU, but then you're exhibiting a Uno.

Yes, the analog out line goes to the A0 pin in the Node.

Okay I will take out the base resistors

I don't know if I need a pull down resistor, I just followed the tutorial mentioned above. But I will try to wire a pulldown resistor, in that aspect, what would be the value? Is 1k to much?

Because I want to send data to a web server using NodeMCU. And I want a lightweight version.
I would like to use the Joystick and NodeMCU to create a sort of controller for a Android Game..

You can tell that tutorial is not the best because he drew the schematic upside down.
Yes 1K will do, you need this as a common reference.

Ads1115?


Adds 4 analog inputs.
Better quality than NodeMCU's built-in analog port
Uses only 2 NodeMCU pins
Those 2 pins can still be used for other i2c devices such as sensors, LCD/oled displays...

You do know that the circuit you are trying to make can never give you the full range of readings from your A/D? This is because a transistor in common emitter mode will always have at least 0.7V between the base and emitter, giving you a fixed offset to any variation you get from your two joystick controls.

This might have been mentioned in that tutorial, but I don’t recall it.