{"id":29,"date":"2020-05-03T20:03:10","date_gmt":"2020-05-03T20:03:10","guid":{"rendered":"https:\/\/samndave.org.uk\/ourdoc\/?p=29"},"modified":"2025-07-10T16:11:35","modified_gmt":"2025-07-10T16:11:35","slug":"microbit-joystick-module","status":"publish","type":"post","link":"https:\/\/samndave.org.uk\/index.php\/2020\/05\/03\/microbit-joystick-module\/","title":{"rendered":"Micro:bit Joystick Module"},"content":{"rendered":"\n<figure class=\"wp-block-image size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/www.makers-hut.com\/wp-content\/uploads\/2019\/01\/Dual-axis-XY-Joystick-Module-PS2-Joystick-Control-Lever-Sensor-For-Arduino-KY-151851053105-3.jpg\" alt=\"\" width=\"153\" height=\"114\"\/><figcaption><em>Dual axis thumb joystick module<\/em><\/figcaption><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">These small joystick modules are composed of a single switch (triggered when pushing down), and two potentiometers set by moving the X and Y axis. As well as two power pins, it has three ouput pins, SW, VRy and VRx. VRy and VRx must be connected to analog capable pins, while SW can be read as a digital value.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The switch is a basic push button which connects SW to GND. In order to function, either an external pull-up resistor must be used to Vcc or make use of the internal pull-ups as demonstrated in the code below.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The following code performs the basic read operations and outputs the joystick status tothe serial console:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># Testing a joystick module\n# Connections:\n#   sw - pin 0\n#   VRy - pin 1\n#   VRx - pin 2\n\nfrom microbit import *\n\njsSW = pin0\njsX = pin2\njsY = pin1\n\n# Configure switch for internal pullup\njsSW.set_pull(jsSW.PULL_UP)\n\nwhile True:\n    sw = jsSW.read_digital()\n    jx = jsX.read_analog()\n    jy = jsY.read_analog()\n    \n    print(\"sw=\"+str(sw)+\", jx=\"+str(jx)+\", jy=\"+str(jy))\n    sleep(500)<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-table is-style-stripes\"><table class=\"has-fixed-layout\"><thead><tr><th>Direction<\/th><th>VRx<\/th><th>VRy<\/th><\/tr><\/thead><tbody><tr><td>Middle<\/td><td>505<\/td><td>497<\/td><\/tr><tr><td>Up<\/td><td>505<\/td><td>1<\/td><\/tr><tr><td>Down<\/td><td>505<\/td><td>1020<\/td><\/tr><tr><td>Left<\/td><td>1<\/td><td>497<\/td><\/tr><tr><td>Right<\/td><td>1020<\/td><td>497<\/td><\/tr><\/tbody><\/table><figcaption>The table shows the values of VRx and VRy depending on the maximum position of the joystick. If you want to read these as absolute directions, pick a threshold between to trigger on.<\/figcaption><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">The following code introduces a function to check the direction based on the above values, then change the LED matrix accordingly.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># Testing a joystick module\n# Connections:\n#   sw - pin 0\n#   VRy - pin 1\n#   VRx - pin 2\n\nfrom microbit import *\n\njsSW = pin0\njsX = pin2\njsY = pin1\n\n# Configure switch for internal pullup\njsSW.set_pull(jsSW.PULL_UP)\n\ndef getJSdir():\n    # Returns joystick direction when pushed towards the limits\n    # 0 = middle, 1 = up, 2 = right, 3 = down, 4 = left\n    jx = jsX.read_analog()\n    jy = jsY.read_analog()\n\n    if(jy &lt; 100):\n        return 1\n    if(jx > 900):\n        return 2\n    if(jy > 900):\n        return 3\n    if(jx &lt; 100):\n        return 4\n        \n    return 0\n    \n\nwhile True:\n    sw = jsSW.read_digital()\n    \n    d = getJSdir()\n    if(d == 0):\n        display.show(Image.HAPPY)\n    if(d == 1):\n        display.show(Image.ARROW_N)\n    if(d == 2):\n        display.show(Image.ARROW_E)\n    if(d == 3):\n        display.show(Image.ARROW_S)\n    if(d == 4):\n        display.show(Image.ARROW_W)\n    \n    if(sw == 0):\n        display.show(Image.COW)\n        \n    sleep(200)\n    <\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>These small joystick modules are composed of a single switch (triggered when pushing down), and [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3,5],"tags":[2,4],"class_list":["post-29","post","type-post","status-publish","format-standard","hentry","category-microbit","category-hardware","tag-hardware","tag-microbit"],"_links":{"self":[{"href":"https:\/\/samndave.org.uk\/index.php\/wp-json\/wp\/v2\/posts\/29","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/samndave.org.uk\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/samndave.org.uk\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/samndave.org.uk\/index.php\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/samndave.org.uk\/index.php\/wp-json\/wp\/v2\/comments?post=29"}],"version-history":[{"count":1,"href":"https:\/\/samndave.org.uk\/index.php\/wp-json\/wp\/v2\/posts\/29\/revisions"}],"predecessor-version":[{"id":1189,"href":"https:\/\/samndave.org.uk\/index.php\/wp-json\/wp\/v2\/posts\/29\/revisions\/1189"}],"wp:attachment":[{"href":"https:\/\/samndave.org.uk\/index.php\/wp-json\/wp\/v2\/media?parent=29"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/samndave.org.uk\/index.php\/wp-json\/wp\/v2\/categories?post=29"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/samndave.org.uk\/index.php\/wp-json\/wp\/v2\/tags?post=29"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}