{"id":480,"date":"2021-01-02T19:20:33","date_gmt":"2021-01-02T17:20:33","guid":{"rendered":"https:\/\/www.bramm.dk\/blog\/?page_id=480"},"modified":"2022-10-06T21:40:29","modified_gmt":"2022-10-06T19:40:29","slug":"interface-to-arduino-uno","status":"publish","type":"page","link":"https:\/\/www.bramm.dk\/blog\/?page_id=480","title":{"rendered":"Interface to Arduino UNO"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">I wanted to connect my Z80\/Z180 systems to Arduino UNO, because sometimes it is an easy path to integrate cheap sensors and analog devices. There are lots of sensors and stuff for Arduino and also much software to drive it. It is definitely not true 8-bit retro-style, but it works.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">I thought it was easy. Just connect cross-wired TX\/RX pins and a GND connection.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Nope.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">After several hours of frustration, I finally found out that Arduino RX pin 0 and TX pin 1 is closely coupled to the USB port of the Arduino UNO, and apparently it is not really suitable for &#8220;general&#8221; serial communications.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">To make it work, choose two different pins, like pin 2 and 3, and connect those to TX\/RX on the Z80\/Z180 serial port. In your sketch, use the library <em>SoftwareSerial <\/em>to define the serial interface on pin 2 and 3 &#8211; that did the trick for me.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/www.bramm.dk\/blog\/wp-content\/uploads\/arduino-sc130-1024x683.jpg\" alt=\"\" class=\"wp-image-481\" width=\"512\" height=\"342\" srcset=\"https:\/\/www.bramm.dk\/blog\/wp-content\/uploads\/arduino-sc130-1024x683.jpg 1024w, https:\/\/www.bramm.dk\/blog\/wp-content\/uploads\/arduino-sc130-300x200.jpg 300w, https:\/\/www.bramm.dk\/blog\/wp-content\/uploads\/arduino-sc130-768x512.jpg 768w, https:\/\/www.bramm.dk\/blog\/wp-content\/uploads\/arduino-sc130-1536x1024.jpg 1536w, https:\/\/www.bramm.dk\/blog\/wp-content\/uploads\/arduino-sc130-2048x1365.jpg 2048w\" sizes=\"auto, (max-width: 512px) 100vw, 512px\" \/><figcaption>Arduino UNO to SC130 Z180 computer<\/figcaption><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">In the picture above it may be a little hard to see, but Arduino (in the bottom) pin 2 goes to TX and pin 3 goes to RX. GND goes to 0V.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">By default the <a href=\"https:\/\/smallcomputercentral.wordpress.com\/sc130-z180-motherboard\/\" target=\"_blank\" rel=\"noreferrer noopener\">SC130<\/a> configures the baudrate to be 115200, which may be a little too high for software serial connection. I reduced speed to 9600, which is absolutely reliable. In CP\/M 3 you can use the MODE command to easily change the baudrate (<code>MODE COM1:9600,N,8,1<\/code>).<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Turbo Pascal Example for CP\/M<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Note that the builtin &#8220;file&#8221; <strong>aux<\/strong> in Turbo Pascal maps to the AUX: device in CP\/M, making it very easy to read\/write the secondary serial port (COM1 on the SC130 in this case). Remember to run the MODE command in CP\/M to set the correct baudrate for the port. This example assumes 9600 baud.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>program echo;\nvar\n  msg: string&#91;132];\nbegin\n  while true do begin\n    writeln(aux,'SC130 speaking\u2026');\n    readln(aux, msg);\n    writeln(msg);\n    delay(2000);\n  end;\nend.<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Arduino Uno Sketch<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">I used pins 2 and 3 in this example, you are free to use any of the digital I\/O pins for software serial communication.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#include &lt;SoftwareSerial.h&gt;\n\n#define RX_PIN (2)\n#define TX_PIN (3)\n#define LED (13)\n#define BUFSIZE (132)\n#define CR (0x0D)\n#define LF (0x0A)\n#define BAUDRATE (9600)\n#define EMPTY_STRING \"\"\n\nSoftwareSerial auxSerial(RX_PIN, TX_PIN);\nString buffer;\n\nvoid setup() {\n  buffer.reserve(BUFSIZE);\n  buffer = EMPTY_STRING;\n  pinMode(LED, OUTPUT);\n  auxSerial.begin(BAUDRATE);\n}\n\nvoid loop() {\n  while (auxSerial.available() &gt; 0) {\n    digitalWrite(LED, HIGH);\n    char ch = auxSerial.read();\n    if (ch == CR) {\n      auxSerial.println(\"Arduino says: \" + buffer);\n      buffer = EMPTY_STRING;\n    } else {\n      if (ch != LF &amp;&amp; buffer.length() &lt; BUFSIZE-1) {\n        buffer = buffer + ch;\n      }\n    }\n    digitalWrite(LED, LOW);\n  }\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n","protected":false},"excerpt":{"rendered":"<p>I wanted to connect my Z80\/Z180 systems to Arduino UNO, because sometimes it is an easy path to integrate cheap sensors and analog devices. There are lots of sensors and stuff for Arduino and also much software to drive it. &hellip; <a href=\"https:\/\/www.bramm.dk\/blog\/?page_id=480\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"parent":0,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"","meta":{"_jetpack_hide_featured_image":false,"_jetpack_featured_image_duplicate_dismissed":0,"footnotes":""},"class_list":["post-480","page","type-page","status-publish","hentry"],"jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/www.bramm.dk\/blog\/index.php?rest_route=\/wp\/v2\/pages\/480","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.bramm.dk\/blog\/index.php?rest_route=\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/www.bramm.dk\/blog\/index.php?rest_route=\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/www.bramm.dk\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.bramm.dk\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=480"}],"version-history":[{"count":5,"href":"https:\/\/www.bramm.dk\/blog\/index.php?rest_route=\/wp\/v2\/pages\/480\/revisions"}],"predecessor-version":[{"id":606,"href":"https:\/\/www.bramm.dk\/blog\/index.php?rest_route=\/wp\/v2\/pages\/480\/revisions\/606"}],"wp:attachment":[{"href":"https:\/\/www.bramm.dk\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=480"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}