{"id":57,"date":"2022-01-30T14:02:00","date_gmt":"2022-01-30T13:02:00","guid":{"rendered":"https:\/\/techblog.dansbo.dk\/?p=57"},"modified":"2024-01-04T14:31:38","modified_gmt":"2024-01-04T13:31:38","slug":"turning-an-arduino-uno-or-nano-into-a-usb-joystick-controller-for-atari-commodore-joysticks","status":"publish","type":"post","link":"https:\/\/techblog.dansbo.dk\/?p=57","title":{"rendered":"Turning an Arduino Uno or Nano into a USB Joystick controller for Atari\/Commodore Joysticks"},"content":{"rendered":"\n<p>I have never been able to get used to playing Commodore 64 games in Vice with the keyboard or a gamepad. I don\u2019t have any USB joysticks and I find the ready-made USB to Atari joystick controllers too expensive.<\/p>\n\n\n\n<p>Then I thought that maybe I could use one of my Arduino Nano\u2019s as a Joystick controller, I did find a couple of article that talk about using the Arduino as joystick controller, but it needs to be specific models that are able to act as a USB keyboard in order to function. I don\u2019t have any of those models.<\/p>\n\n\n\n<p>I had almost given up when I stumbled upon a post in the\u00a0<a href=\"https:\/\/forum.arduino.cc\/\">Arduino Forums<\/a>\u00a0named\u00a0<a href=\"https:\/\/forum.arduino.cc\/t\/turning-an-arduino-nano-into-a-joystick\/266492\/5\">Turning an Arduino nano into a joystick<\/a>. This was exactly what I was looking for and it works.<\/p>\n\n\n\n<p>I have basically copied the content from that post, some I have changed to fit my own needs and some I believe I have improved upon.<\/p>\n\n\n\n<p>We need to know pinout of the DB9 connector. Fortunately Wikipedia has us covered with\u00a0<a href=\"https:\/\/en.wikipedia.org\/wiki\/Atari_joystick_port\">an article describing just that<\/a>. This picture is taken from there:<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"258\" height=\"323\" src=\"https:\/\/techblog.dansbo.dk\/wp-content\/uploads\/2024\/01\/image.png\" alt=\"\" class=\"wp-image-58\" srcset=\"https:\/\/techblog.dansbo.dk\/wp-content\/uploads\/2024\/01\/image.png 258w, https:\/\/techblog.dansbo.dk\/wp-content\/uploads\/2024\/01\/image-240x300.png 240w\" sizes=\"auto, (max-width: 258px) 100vw, 258px\" \/><\/figure>\n<\/div>\n\n\n<p>When a button is pressed or joystick i pushed in a direction, the corresponding pin is pulled to ground. This means that all we need to do is connect ground and the different direction- and trigger- pins to the arduino.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"587\" height=\"713\" src=\"https:\/\/techblog.dansbo.dk\/wp-content\/uploads\/2024\/01\/image-1.png\" alt=\"\" class=\"wp-image-59\" srcset=\"https:\/\/techblog.dansbo.dk\/wp-content\/uploads\/2024\/01\/image-1.png 587w, https:\/\/techblog.dansbo.dk\/wp-content\/uploads\/2024\/01\/image-1-247x300.png 247w\" sizes=\"auto, (max-width: 587px) 100vw, 587px\" \/><\/figure>\n<\/div>\n\n\n<p>I was unable to find a symbol for the DB9 plug as well as the Arduino Nano so you will just have to use your imagination or below table.<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td><strong>DB9 pin<\/strong><\/td><td><strong>Arduino pin<\/strong><\/td><td><strong>Description<\/strong><\/td><\/tr><tr><td>8<\/td><td>GND<\/td><td>Ground connection<\/td><\/tr><tr><td>6<\/td><td>2<\/td><td>Fire\/Trigger button<\/td><\/tr><tr><td>4<\/td><td>3<\/td><td>Right<\/td><\/tr><tr><td>3<\/td><td>4<\/td><td>Left<\/td><\/tr><tr><td>2<\/td><td>5<\/td><td>Down<\/td><\/tr><tr><td>1<\/td><td>6<\/td><td>Up<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>Once things are wired up, it is time to do a bit of programming of the arduino.<\/p>\n\n\n\n<script src=\"https:\/\/pasteit.dansbo.dk\/\/paste.php?embed&#038;id=14\"><\/script>\n\n\n\n<p>Above code uses only 5 digital inputs and serial communication so it should be able to run on a variety of Arduino\u2019s. I have only been able to test it on the Uno and Nano.<\/p>\n\n\n\n<p>On the PC end, I am running Linux so a bash script will convert the serial data from the Arduino to keypresses. This is done by the use of the\u00a0<strong>xdotool<\/strong>.<\/p>\n\n\n\n<p>When you are ready to play, you start ardujoy.sh in a shell, fire up Vice and ensure that your joystick uses a keyset. Then all you have to do is configure the keyset the same way as described in the Bash script and you are good to go.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"198\" height=\"399\" src=\"https:\/\/techblog.dansbo.dk\/wp-content\/uploads\/2024\/01\/image-2.png\" alt=\"\" class=\"wp-image-60\" srcset=\"https:\/\/techblog.dansbo.dk\/wp-content\/uploads\/2024\/01\/image-2.png 198w, https:\/\/techblog.dansbo.dk\/wp-content\/uploads\/2024\/01\/image-2-149x300.png 149w\" sizes=\"auto, (max-width: 198px) 100vw, 198px\" \/><\/figure>\n<\/div>\n\n\n<p><s>I am unsure if there are any tools for Windows that are able to read input from a serial port and simulate keypresses.<\/s><\/p>\n\n\n\n<p>UPDATE: I have created a quick\u2019n\u2019dirty program for Windows that will read a serial port and emulate keypresses just like above bash script. The program is developed using\u00a0<a href=\"https:\/\/en.wikipedia.org\/wiki\/SharpDevelop\">SharpDevelop<\/a>, it must have a serial port name as the only option. eg:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>ardujoy.exe COM1<\/code><\/pre>\n\n\n\n<script src=\"https:\/\/pasteit.dansbo.dk\/\/paste.php?embed&#038;id=17\"><\/script>\n\n\n\n<p>If you do not want to install Sharpdevelop, create a console application and paste above code, you can download the program\u00a0<a href=\"https:\/\/cloud.dansbo.dk\/index.php\/s\/yWMiXWiT3mQFH68\">here<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I have never been able to get used to playing Commodore 64 games in Vice with the keyboard or a gamepad. I don\u2019t have any USB joysticks and I find&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-57","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/techblog.dansbo.dk\/index.php?rest_route=\/wp\/v2\/posts\/57","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/techblog.dansbo.dk\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/techblog.dansbo.dk\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/techblog.dansbo.dk\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/techblog.dansbo.dk\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=57"}],"version-history":[{"count":1,"href":"https:\/\/techblog.dansbo.dk\/index.php?rest_route=\/wp\/v2\/posts\/57\/revisions"}],"predecessor-version":[{"id":61,"href":"https:\/\/techblog.dansbo.dk\/index.php?rest_route=\/wp\/v2\/posts\/57\/revisions\/61"}],"wp:attachment":[{"href":"https:\/\/techblog.dansbo.dk\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=57"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/techblog.dansbo.dk\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=57"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/techblog.dansbo.dk\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=57"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}