{"id":58,"date":"2022-10-02T14:48:46","date_gmt":"2022-10-02T14:48:46","guid":{"rendered":"https:\/\/engprojects.tcnj.edu\/rover-rescue\/?p=58"},"modified":"2022-10-02T23:01:14","modified_gmt":"2022-10-02T23:01:14","slug":"raspberry-pi-4-to-arduino-mega-serial-communication","status":"publish","type":"post","link":"https:\/\/engprojects.tcnj.edu\/rover-rescue\/2022\/10\/02\/raspberry-pi-4-to-arduino-mega-serial-communication\/","title":{"rendered":"Project Update 4: Raspberry Pi 4 to Arduino Mega Serial Communication"},"content":{"rendered":"<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/engprojects.tcnj.edu\/rover-rescue\/wp-content\/uploads\/sites\/223\/2022\/10\/IMG-0920-1024x501.jpg\" alt=\"\" class=\"wp-image-65\" width=\"554\" height=\"270\" srcset=\"https:\/\/engprojects.tcnj.edu\/rover-rescue\/wp-content\/uploads\/sites\/223\/2022\/10\/IMG-0920-1024x501.jpg 1024w, https:\/\/engprojects.tcnj.edu\/rover-rescue\/wp-content\/uploads\/sites\/223\/2022\/10\/IMG-0920-300x147.jpg 300w, https:\/\/engprojects.tcnj.edu\/rover-rescue\/wp-content\/uploads\/sites\/223\/2022\/10\/IMG-0920-768x376.jpg 768w, https:\/\/engprojects.tcnj.edu\/rover-rescue\/wp-content\/uploads\/sites\/223\/2022\/10\/IMG-0920-1536x751.jpg 1536w, https:\/\/engprojects.tcnj.edu\/rover-rescue\/wp-content\/uploads\/sites\/223\/2022\/10\/IMG-0920-2048x1002.jpg 2048w\" sizes=\"auto, (max-width: 554px) 100vw, 554px\" \/><figcaption>Figure 1: Raspberry Pi and Arduino Mega Serial Communication via USB<\/figcaption><\/figure><\/div>\n\n\n<p>The group was able to send messages between the Raspberry Pi 4 and Arduino Mega via serial communication.  A python script running on the Pi accepts a number from the command line and sends it to the Mega using a popular Python library for sending serial information called PySerial. The Mega then increments the number by 1 and sends it back to the Raspberry Pi which will display the new number to the terminal. <\/p>\n\n\n\n<p>The serial.Serial(&#8230;) command on line 3 opens serial communication on port \/dev\/ttyACM0 because the Mega was connected to port \/dev\/ttyACM0 on the Pi. Notice how the baud rate on the Pi must match the baud rate on the Mega (115,200 Hz). <\/p>\n\n\n\n<p>The terminal shows the result of running the Python code after uploading the Arduino .ino file to the Mega. <\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Python Code<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>import serial\nimport time\narduino = serial.Serial(port='\/dev\/ttyACM0', baudrate=115200, timeout=.1)\ndef write_read(x):\n    arduino.write(bytes(x, 'utf-8'))\n    time.sleep(0.05)\n    data = arduino.readline()\n    return data\nwhile True:\n    num = input(\"Enter a number: \") # Taking input from user\n    value = write_read(num)\n    print(value) # printing the value<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Arduino Code<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>int x;\n\nvoid setup() {\n  Serial.begin(115200);\n  Serial.setTimeout(1);\n}\n\nvoid loop() {\n  while (!Serial.available());\n  x = Serial.readString().toInt();\n  Serial.print(x + 1);\n}<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Running PySerial in Raspberry Pi Terminal<\/h2>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"736\" height=\"480\" src=\"https:\/\/engprojects.tcnj.edu\/rover-rescue\/wp-content\/uploads\/sites\/223\/2022\/10\/pyserial.png\" alt=\"\" class=\"wp-image-59\" srcset=\"https:\/\/engprojects.tcnj.edu\/rover-rescue\/wp-content\/uploads\/sites\/223\/2022\/10\/pyserial.png 736w, https:\/\/engprojects.tcnj.edu\/rover-rescue\/wp-content\/uploads\/sites\/223\/2022\/10\/pyserial-300x196.png 300w\" sizes=\"auto, (max-width: 736px) 100vw, 736px\" \/><\/figure>\n","protected":false},"excerpt":{"rendered":"<p>The group was able to send messages between the Raspberry Pi 4 and Arduino Mega via serial communication. A python script running on the Pi accepts a number from the command line and sends it to the Mega using a popular Python library for sending serial information called PySerial. The Mega then increments the number &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/engprojects.tcnj.edu\/rover-rescue\/2022\/10\/02\/raspberry-pi-4-to-arduino-mega-serial-communication\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Project Update 4: Raspberry Pi 4 to Arduino Mega Serial Communication&#8221;<\/span><\/a><\/p>\n","protected":false},"author":438,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"ngg_post_thumbnail":0,"footnotes":""},"categories":[1],"tags":[],"class_list":["post-58","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/engprojects.tcnj.edu\/rover-rescue\/wp-json\/wp\/v2\/posts\/58","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/engprojects.tcnj.edu\/rover-rescue\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/engprojects.tcnj.edu\/rover-rescue\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/engprojects.tcnj.edu\/rover-rescue\/wp-json\/wp\/v2\/users\/438"}],"replies":[{"embeddable":true,"href":"https:\/\/engprojects.tcnj.edu\/rover-rescue\/wp-json\/wp\/v2\/comments?post=58"}],"version-history":[{"count":0,"href":"https:\/\/engprojects.tcnj.edu\/rover-rescue\/wp-json\/wp\/v2\/posts\/58\/revisions"}],"wp:attachment":[{"href":"https:\/\/engprojects.tcnj.edu\/rover-rescue\/wp-json\/wp\/v2\/media?parent=58"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/engprojects.tcnj.edu\/rover-rescue\/wp-json\/wp\/v2\/categories?post=58"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/engprojects.tcnj.edu\/rover-rescue\/wp-json\/wp\/v2\/tags?post=58"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}