Close
0%
0%

2 Analog Inputs for ESP8266 Without Multiplexer

Inputting 2 analog inputs into the ESP8266 without a multiplexer using just a couple of IO.

Similar projects worth following
I've been wanting to make a gameboy using the ESP8266 ever since I discovered the i2c OLED display, because the ESP8266 has built in wifi and an actually pretty powerful microcontroller built in. meaning i could create multiple of them and create multiplayer games. I hope to eventually port over the Super Smash Bros. Open that they have for the TI-84, and eventually create pcbs and sell these as kits. The first obstacle I needed to overcome was I wanted to use an analog joystick with the ESP8266 but the ESP8266 only has 1 analog input. The way this is traditionally overcome is with a multiplexer but I didn't want to add another IC to my BOM. So in this project I will explain how I used the analog input along with 2 GPIO pins and a couple of resistors to expand the ESP8266 analog input to 2 inputs

Firstly I hooked up the joystick as shown in the schematic. The two 10k resistors coming from Joystick X and Y are there so when the digital GPIO is sinked to GND the Analog input is not also sinked completely to ground. to check for X GPIO 13 is set to floating and GPIO 14 is set to GND. using those 2 resistors along the voltage at the ADC will be half of what is coming from the Joystick X which will be a maximum of 1.65V. Since the ADC will only read to a maximum of 1 Volt, this voltage needs to be brought down even more. An additional 10K resistor is used to bring the voltage down even further. Ideally this should be a smaller value but in testing 10K seemed to be adequate and this further brought down the BOM.

Using this code I was able to make a function to calibrate the joystick by finding it's maximum and minimum values for both the X and Y and then creating functions for getting the X and the Y values.

int    joystick_xmax = 1023;
float  joystick_xmult = 1;
int    joystick_ymax = 1023;
float  joystick_ymult = 1;

static void calibrate()
{
  int cnt = 0;
  int xmax = 0;
  int xmin = 1023;
  int ymax = 0;
  int ymin = 1023;
  while(cnt < 4)
  {
     int jx = joystick_xmax-get_joystick_x();
     int jy = joystick_ymax-get_joystick_y();
     if((cnt % 2 == 0) && jx < 450) 
       cnt++;
     if((cnt % 2 == 1) && jx > 800) 
       cnt++;
     xmax = max(xmax, jx);
     xmin = min(xmin, jx);
     ymax = max(ymax, jy);
     ymin = min(ymin, jy);
  }
  joystick_xmax = xmax;
  joystick_xmult = 1023.0/(xmax - xmin);
  joystick_ymax = ymax;
  joystick_ymult = 1023.0/(ymax - ymin);
}

static unsigned int get_joystick_x()
{
  pinMode(14,INPUT);
  pinMode(13,OUTPUT);
  digitalWrite(13,LOW);
  return (unsigned int)max(0, min(1023,((joystick_xmax-analogRead(A0))*joystick_xmult)));
}
static unsigned int get_joystick_y()
{
  pinMode(13,INPUT);
  pinMode(14,OUTPUT);
  digitalWrite(14,LOW);
  return (unsigned int)max(0, min(1023,((joystick_ymax-analogRead(A0))*joystick_ymult)));
}

Here are some videos of me testing the circuits.

Bread-boarding the circuit.

OLED pong ported over to it using the joystick.

Overview of the fully featured game module. Will be creating PCBs for it soon.

View project log

Enjoy this project?

Share

Discussions

Mike Causer wrote 07/15/2016 at 02:45 point

Nice work!

  Are you sure? yes | no

josephchrzempiec wrote 05/25/2016 at 05:58 point

Very awesome man :)

  Are you sure? yes | no

Electromania wrote 12/03/2015 at 16:32 point

Amazing idea and short and simple solution :).

Sorry for basic question, I am a newbee, how did you manage to display it on your oled display, which library ? can you share that code too ?

  Are you sure? yes | no

Craig Hissett wrote 11/15/2015 at 22:03 point

Great project - I look forward to seeing how your device takes shape once you have designed your custom pcbs. Im especially looking forward to seeing how your software develops. It would be great to develop/port games for it, plus more - the potential for a small, wifi-enabled control device is unreal!

Keep up the great work!

  Are you sure? yes | no

Daniel Johnson wrote 11/16/2015 at 05:16 point

Thanks so much! i plan on making a separate project log for it as well. I might get around to the custom pcb design tonight

  Are you sure? yes | no

Craig Hissett wrote 11/16/2015 at 06:41 point

Tremendous!

  Are you sure? yes | no

Daniel Johnson wrote 11/16/2015 at 07:32 point

let me know what you think http://imgur.com/4E2oG6U

  Are you sure? yes | no

Craig Hissett wrote 11/16/2015 at 07:45 point

I think it looks brilliant mate; but I am no expert on pcb design. Maybe @davedarko will be able to give you better feedback ha ha.

One thing - how difficult would it be to provide an extra i2c port on your pcb design? Could be useful as an expansion port.

  Are you sure? yes | no

davedarko wrote 11/16/2015 at 10:26 point

looks like you should add some pullup resistors on the switches as well.

  Are you sure? yes | no

Daniel Johnson wrote 11/16/2015 at 08:13 point

great idea! however it would make the pcb a bit bigger and the goal is to make this as small as possible. however you can easily add a header on the bottom side of the i2c display as it would make the perfect connector for expansion. you could even solder i2c expansions underneath the display like acceleromator/tilt sensor. ill add an extra set of pads underneath the screen in case anyone would like to do that as it will use some  currently unused space

  Are you sure? yes | no

Craig Hissett wrote 11/16/2015 at 08:16 point

That sounds tremendous!

If you can fit the pads on without too much hassle J definitely think it will make a great addition. There's hundreds of i2c devices that could be used with this :)

  Are you sure? yes | no

Daniel Johnson wrote 11/16/2015 at 08:30 point

actually now that i think about it. when the i2c display is soldered into the board. the extra bit of header can be used as a male header for an expansion port. also the extra header pads underneat the OLED can be soldered in at 90 degrees and could make a very nice expansion port as well

  Are you sure? yes | no

davedarko wrote 11/16/2015 at 08:43 point

I2c eeprom games ;) ;)

  Are you sure? yes | no

davedarko wrote 11/15/2015 at 01:11 point

cool project :) do you have any problems with the display initialization? I had the problem that it sometimes wouldn't show stuff the first time and I had to reset the arduino again. Now I'm using a resistor and a cap to keep the reset low on startup.

  Are you sure? yes | no

Daniel Johnson wrote 11/15/2015 at 05:47 point

I haven't run into any of those problems, the OLED I have only has V+ GND SDA SCL. it doesn't have any other pins like some of the other ones out there.

  Are you sure? yes | no

davedarko wrote 11/15/2015 at 07:37 point

That's why I'm asking, my one looks the same.

  Are you sure? yes | no

Similar Projects

Does this project spark your interest?

Become a member to follow this project and never miss any updates