{"id":1388,"date":"2020-10-30T06:56:05","date_gmt":"2020-10-30T11:56:05","guid":{"rendered":"https:\/\/www.tigoe.com\/pcomp\/code\/?p=1388"},"modified":"2020-10-30T08:26:59","modified_gmt":"2020-10-30T13:26:59","slug":"serial-communication-sending-a-10-bit-or-two-byte-value","status":"publish","type":"post","link":"https:\/\/www.tigoe.com\/pcomp\/code\/arduinowiring\/1388\/","title":{"rendered":"Serial Communication: Sending a 10-Bit (or two-byte) Value"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">When communicating between a microcontroller and a personal computer using asynchronous serial communication, you often need to transmit a value that is greater than 255. For example, if you want to send the value from a 10-bit analog-to-digital converter (e.g. the result from the Arduino analogRead() command) as a binary value, here&#8217;s how you do it:<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><code>int sensor = analogRead(A0);<\/code><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The int data type of this variable takes multiple bytes in the Arduino&#8217;s memory. Reading the value 1023 from the ADC, for example, this is the arrangement of the bits in the bytes  in memory: <\/p>\n\n\n\n<figure class=\"wp-block-table is-style-regular\"><table class=\"has-fixed-layout\"><tbody><tr><td><\/td><td>first byte<\/td><td>second byte<\/td><\/tr><tr><td>Bit values of the byte:<\/td><td>0 0 0 0 0 0 1 1<\/td><td>1 1 1 1 1 1 1 1<\/td><\/tr><tr><td>Decimal value of the byte<\/td><td>3<\/td><td>255<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">To separate the original value into two bytes, you divide by 256 to get the first byte, and take the remainder (or modulo) to get the second byte.  For example, 1023 \/ 256 = 3, remainder 255.  You can see this in the Arduino code below. To send this value as two bytes from Arduino, you do the following:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: arduino; title: ; notranslate\" title=\"\">\n \/\/ get a 10-bit ADC value:\n  int sensor = analogRead(A0);\n  \/\/ divide by 256 to get the first byte:\n  \/\/ (result is an integer):\n  byte firstByte = sensor \/ 256;\n  \/\/ modulo by 256 to get the second byte:\n  byte secondByte = sensor % 256;\n  \/\/ send the first of the two bytes:\n  Serial.write(firstByte);\n  \/\/ send the second of the two bytes:\n  Serial.write(secondByte);\n<\/pre><\/div>\n\n\n<p class=\"wp-block-paragraph\">When you receive this on the personal computer, you need to combine the two bytes back into a single value. To do this, you multiply the first byte by 256, then add the second byte.  For example, 3 * 256 + 255 = 1023. Here&#8217;s how to do it in <a href=\"https:\/\/p5js.org\/\">p5.js<\/a> using <a href=\"https:\/\/p5-serial.github.io\/\">p5.serial;ort<\/a>:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\n  \/\/ wait until you have two bytes:\n  if (serial.available() &gt;= 2) {\n    \/\/ read the first byte:\n    var highByte = serial.read();\n    \/\/ read the second byte:\n    var lowByte = serial.read();\n    \/\/ combine them into a single value:\n    var result = (highByte * 256) + lowByte;\n    \/\/ put the result in the text div:\n    console.log(&quot;high byte: &quot; + highByte +\n      &quot;  low byte: &quot; + lowByte +\n      &quot;  result: &quot; + result);\n  }\n<\/pre><\/div>\n\n\n<p class=\"wp-block-paragraph\">Here are the two full sketches for both Arduino and p5.js:<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Arduino:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: arduino; title: ; notranslate\" title=\"\">\nvoid setup() {\n  Serial.begin(9600);\n}\n\nvoid loop() {\n  \/\/ get a 10-bit ADC value:\n  int sensor = analogRead(A0);\n  \/\/ divide by 256 to get the first byte:\n  \/\/ (result is an integer):\n  byte firstByte = sensor \/ 256;\n  \/\/ modulo by 256 to get the second byte:\n  byte secondByte = sensor % 256;\n  \/\/ send the first of the two bytes:\n  Serial.write(firstByte);\n  \/\/ send the second of the two bytes:\n  Serial.write(secondByte);\n}\n<\/pre><\/div>\n\n\n<p class=\"wp-block-paragraph\">The p5.js sketch:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\n\/\/ variable to hold an instance of the serialport library:\nlet serial;\n\/\/ HTML Select option object:\nlet portSelector;\n\n\nfunction setup() {\n  \/\/ new instance of the serialport library\n  serial = new p5.SerialPort();\n  \/\/ callback function for serialport list event\n  serial.on(&#039;list&#039;, printList);\n  \/\/ callback function for serialport data event\n  serial.on(&#039;data&#039;, serialEvent);\n  \/\/ list the serial ports\n  serial.list();\n  textDiv = createDiv(&#039;result will go here.&#039;)\n}\n\n\/\/ make a serial port selector object:\nfunction printList(portList) {\n  \/\/ create a select object:\n  portSelector = createSelect();\n  portSelector.position(10, 10);\n  \/\/ portList is an array of serial port names\n  for (var i = 0; i &lt; portList.length; i++) {\n    \/\/ add this port name to the select object:\n    portSelector.option(portList&#x5B;i]);\n  }\n  \/\/ set an event listener for when the port is changed:\n  portSelector.changed(mySelectEvent);\n}\n\n\nfunction mySelectEvent() {\n  let item = portSelector.value();\n  \/\/ give it an extra property for hiding later:\n  portSelector.visible = true;\n  \/\/ if there&#039;s a port open, close it:\n  if (serial.serialport != null) {\n    serial.close();\n  }\n  \/\/ open the new port:\n  serial.open(item);\n}\n\n\nfunction serialEvent() {\n  \/\/ if you have received two or more bytes:\n  if (serial.available() &gt;= 2) {\n    \/\/ read the first byte:\n    var highByte = serial.read();\n    \/\/ read the second byte:\n    var lowByte = serial.read();\n    \/\/ combine them into a single value:\n    var result = (highByte * 256) + lowByte;\n    \/\/ put the result in the text div:\n    console.log(&quot;high byte: &quot; + highByte +\n      &quot;  low byte: &quot; + lowByte +\n      &quot;  result: &quot; + result);\n  }\n}\n<\/pre><\/div>\n\n\n<p class=\"wp-block-paragraph\">Here&#8217;s the index.html page:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: xml; title: ; notranslate\" title=\"\">\n&lt;!DOCTYPE html&gt;&lt;html lang=&quot;en&quot;&gt;&lt;head&gt;\n  &lt;script src=&quot;https:\/\/cdnjs.cloudflare.com\/ajax\/libs\/p5.js\/1.1.9\/p5.min.js&quot; integrity=&quot;sha512-WIklPM6qPCIp6d3fSSr90j+1unQHUOoWDS4sdTiR8gxUTnyZ8S2Mr8e10sKKJ\/bhJgpAa\/qG068RDkg6fIlNFA==&quot; crossorigin=&quot;anonymous&quot;&gt;&lt;\/script&gt;\n  &lt;script language=&quot;javascript&quot; type=&quot;text\/javascript&quot; src=&quot;https:\/\/cdn.jsdelivr.net\/npm\/p5.serialserver@0.0.29\/lib\/p5.serialport.js&quot;&gt;&lt;\/script&gt;\n    &lt;meta charset=&quot;utf-8&quot;&gt;\n  &lt;\/head&gt;\n  &lt;body&gt;\n    &lt;script src=&quot;sketch.js&quot;&gt;&lt;\/script&gt;\n&lt;\/body&gt;&lt;\/html&gt;\n<\/pre><\/div>\n\n\n<p class=\"wp-block-paragraph\">You will also need the <a href=\"https:\/\/github.com\/p5-serial\/p5.serialcontrol\/releases\">p5.serialcontrol<\/a> app to connect the sketch in a browser to the serial port. <\/p>\n","protected":false},"excerpt":{"rendered":"<p>When communicating between a microcontroller and a personal computer using asynchronous serial communication, you often need to transmit a value that is greater than 255. For example, if you want to send the value from a 10-bit analog-to-digital converter (e.g. the result from the Arduino analogRead() command) as a binary value, here&#8217;s how you do &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/www.tigoe.com\/pcomp\/code\/arduinowiring\/1388\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Serial Communication: Sending a 10-Bit (or two-byte) Value&#8221;<\/span><\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[9,103],"tags":[],"class_list":["post-1388","post","type-post","status-publish","format-standard","hentry","category-arduinowiring","category-javascript"],"_links":{"self":[{"href":"https:\/\/www.tigoe.com\/pcomp\/code\/wp-json\/wp\/v2\/posts\/1388","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.tigoe.com\/pcomp\/code\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tigoe.com\/pcomp\/code\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tigoe.com\/pcomp\/code\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tigoe.com\/pcomp\/code\/wp-json\/wp\/v2\/comments?post=1388"}],"version-history":[{"count":7,"href":"https:\/\/www.tigoe.com\/pcomp\/code\/wp-json\/wp\/v2\/posts\/1388\/revisions"}],"predecessor-version":[{"id":1396,"href":"https:\/\/www.tigoe.com\/pcomp\/code\/wp-json\/wp\/v2\/posts\/1388\/revisions\/1396"}],"wp:attachment":[{"href":"https:\/\/www.tigoe.com\/pcomp\/code\/wp-json\/wp\/v2\/media?parent=1388"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tigoe.com\/pcomp\/code\/wp-json\/wp\/v2\/categories?post=1388"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tigoe.com\/pcomp\/code\/wp-json\/wp\/v2\/tags?post=1388"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}