Problem with analog joystick

The official pyboard running MicroPython.
This is the reference design and main target board for MicroPython.
You can buy one at the store.
Target audience: Users with a pyboard.
riklaunim
Posts: 32
Joined: Fri Aug 22, 2014 5:42 pm

Problem with analog joystick

Post by riklaunim » Fri Sep 26, 2014 5:25 pm

I have a typical analog joystick board: two pins for power, one for button pressed, two for x and y axis potentiometer value. So I set two ADC pins to read value returned from potentiometers and one digital input to read button pressed status:

Code: Select all

class Joystick:
    def __init__(self, x_axis_pin, y_axis_pin, button_pin):
        self.x_axis = pyb.ADC(x_axis_pin)
        self.y_axis = pyb.ADC(y_axis_pin)
        self.button = pyb.Pin(button_pin, pyb.Pin.IN)

    def get_state(self):
        return {
            'x': self.x_axis.read(),
            'y': self.y_axis.read(),
            'button': self.button.value()
        }
The problem is that:
- pressing button doesn't set the pin to high
- ADC changes only for one axis on the joystick and it changes values for x and y:

Code: Select all

# start
{'x': 2855, 'button': 0, 'y': 2851}
>>> j.get_state()
# x axis on one side
{'x': 4095, 'button': 0, 'y': 3140}
>>> j.get_state()
# x axis on another side
{'x': 5, 'button': 0, 'y': 5}
One is on X12 second is on Y12 and button is on X1. The same joystick works with pymcu (5V):

Code: Select all

from time import sleep

import pymcu

BUTTON_DIGITAL_PIN = 1
X_AXIS_ANALOG_PIN = 1
Y_AXIS_ANALOG_PIN = 2

mb = pymcu.mcuModule()
mb.digitalState(BUTTON_DIGITAL_PIN, 'input')
mb.pinHigh(BUTTON_DIGITAL_PIN)

while True:
    print mb.analogRead(X_AXIS_ANALOG_PIN), mb.analogRead(Y_AXIS_ANALOG_PIN)
    if not mb.digitalRead(BUTTON_DIGITAL_PIN):
        print 'button pressed'
    sleep(0.1)

User avatar
dhylands
Posts: 3821
Joined: Mon Jan 06, 2014 6:08 pm
Location: Peachland, BC, Canada
Contact:

Re: Problem with analog joystick

Post by dhylands » Mon Sep 29, 2014 8:15 pm

As far as the push button goes, I think you need a pullup.

The lines:

Code: Select all

mb.digitalState(BUTTON_DIGITAL_PIN, 'input')
mb.pinHigh(BUTTON_DIGITAL_PIN)
are configuring the pin as an input and then enabling the pull-up.

You used:

Code: Select all

self.button = pyb.Pin(button_pin, pyb.Pin.IN)
which enables no pull-up. Changing this to:

Code: Select all

self.button = pyb.Pin(button_pin, pyb.Pin.IN, pull=pyb.Pin.PULL_UP)
will enable the pull-up in micropython.

I wired up the following:
JoyStick.png
Simple Joystick
JoyStick.png (21.78 KiB) Viewed 9564 times
and ran this code:

Code: Select all

import pyb

x_pin = pyb.Pin('X12')
y_pin = pyb.Pin('Y12')
button_pin = pyb.Pin('X1')

class Joystick:
    def __init__(self, x_axis_pin, y_axis_pin, button_pin):
        self.x_axis = pyb.ADC(x_axis_pin)
        self.y_axis = pyb.ADC(y_axis_pin)
        self.button = pyb.Pin(button_pin, pyb.Pin.IN, pull=pyb.Pin.PULL_UP)

    def get_state(self):
        return {
            'x': self.x_axis.read(),
            'y': self.y_axis.read(),
            'button': self.button.value()
        }

j = Joystick(x_pin, y_pin, button_pin)

while True:
    print(j.get_state())
    pyb.delay(100)
and I can get this output:

Code: Select all

{'x': 4, 'button': 1, 'y': 1}
{'x': 4, 'button': 1, 'y': 0}
{'x': 4, 'button': 1, 'y': 1}
{'x': 4, 'button': 1, 'y': 4}
{'x': 4, 'button': 1, 'y': 3}
... move one pot ...
{'x': 9, 'button': 1, 'y': 2}
{'x': 493, 'button': 1, 'y': 1}
{'x': 1015, 'button': 1, 'y': 2}
{'x': 1857, 'button': 1, 'y': 1}
{'x': 1855, 'button': 1, 'y': 0}
... move the other pot ...
{'x': 1860, 'button': 1, 'y': 0}
{'x': 1860, 'button': 1, 'y': 3}
{'x': 1860, 'button': 1, 'y': 649}
{'x': 1858, 'button': 1, 'y': 1213}
{'x': 1862, 'button': 1, 'y': 1473}
{'x': 1865, 'button': 1, 'y': 2080}
{'x': 1858, 'button': 1, 'y': 2077}
... press the button ....
{'x': 1858, 'button': 1, 'y': 2080}
{'x': 1858, 'button': 0, 'y': 2080}
{'x': 1860, 'button': 0, 'y': 2077}
... release the button ...
{'x': 1858, 'button': 0, 'y': 2077}
{'x': 1859, 'button': 1, 'y': 2075}
{'x': 1863, 'button': 1, 'y': 2077}
{'x': 1862, 'button': 1, 'y': 2077}
Do you have a schematic for your joystick?

User avatar
dhylands
Posts: 3821
Joined: Mon Jan 06, 2014 6:08 pm
Location: Peachland, BC, Canada
Contact:

Re: Problem with analog joystick

Post by dhylands » Mon Sep 29, 2014 8:21 pm

And what version of the micropython firmware were you using?

Mine reports:

Code: Select all

Micro Python v1.3.1-75-g8369559 on 2014-09-13; PYBv1.0 with STM32F405RG
Type "help()" for more information.
>>> 
There was a bug in ADC related to using multiple channels fixed on June 16. So if your firmware is earlier than that, then I think you'll need to update the firmware.
https://github.com/micropython/micropython/pull/698

riklaunim
Posts: 32
Joined: Fri Aug 22, 2014 5:42 pm

Re: Problem with analog joystick

Post by riklaunim » Tue Sep 30, 2014 6:43 am

Kickstarter version, so I'll probably will have to update it and see if that helps :)

riklaunim
Posts: 32
Joined: Fri Aug 22, 2014 5:42 pm

Re: Problem with analog joystick

Post by riklaunim » Sun Oct 05, 2014 10:38 pm

Firmware updated.

But firmware backup:

Code: Select all

sudo dfu-util --alt 0 --upload pyboard-original.dfu -s:524288
Gave:

Code: Select all

invalid dfuse address: :524288
And there is no description what's that address is and from where it comes from.

User avatar
dhylands
Posts: 3821
Joined: Mon Jan 06, 2014 6:08 pm
Location: Peachland, BC, Canada
Contact:

Re: Problem with analog joystick

Post by dhylands » Mon Oct 06, 2014 2:28 am

There really isn't any reason to backup the firmware.

If you really need to go back, I'd be happy to build you the version that came with your board.

But there really isn't any reason to go back. If for some reason the very latest doesn't work, you can find a few days, plus several previous releases here:
http://micropython.org/download/

The 0.5 version of dfu-util fails the way you described. The latest version works properly.

You can grab source for dfu-util from here:
http://dfu-util.gnumonks.org/

and the build instructions are here:
http://dfu-util.gnumonks.org/build.html

I did the following (ubuntu 14.04)

Code: Select all

git clone git://gitorious.org/dfu-util/dfu-util.git
cd dfu-util
sudo apt-get build-dep dfu-util
sudo apt-get install libusb-1.0-0-dev
./autogen.sh
./configure  # on most systems
make
The compiled dfu-util will be in the src tree.

riklaunim
Posts: 32
Joined: Fri Aug 22, 2014 5:42 pm

Re: Problem with analog joystick

Post by riklaunim » Mon Oct 06, 2014 6:51 pm

I was just doing what https://github.com/micropython/micropyt ... are-Update says to do :]

blmorris
Posts: 348
Joined: Fri May 02, 2014 3:43 pm
Location: Massachusetts, USA

Re: Problem with analog joystick

Post by blmorris » Mon Oct 06, 2014 8:15 pm

riklaunim wrote:I was just doing what https://github.com/micropython/micropyt ... are-Update says to do :]
From the page:
Previous releases of micropython are available from: http://micropython.org/download/ so performing a backup should be unecessary.
Looks like "should be necessary" should have been "shouldn't be necessary"
Guess I'll head over and see if I'm allowed to edit :)

User avatar
dhylands
Posts: 3821
Joined: Mon Jan 06, 2014 6:08 pm
Location: Peachland, BC, Canada
Contact:

Re: Problem with analog joystick

Post by dhylands » Mon Oct 06, 2014 8:45 pm

@blmorris - thanks for the typo correction

riklaunim
Posts: 32
Joined: Fri Aug 22, 2014 5:42 pm

Re: Problem with analog joystick

Post by riklaunim » Sun Oct 12, 2014 6:43 pm

And the firmware update solves problems with ADC as well the Timer class allows better DC motor control :) -> article

Post Reply