Skip to content

Instantly share code, notes, and snippets.

@damontallen
Last active May 30, 2016 06:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save damontallen/5564886 to your computer and use it in GitHub Desktop.
Save damontallen/5564886 to your computer and use it in GitHub Desktop.
This is an IPython Notebook that contains a proof of concept of communicating with an Arduino.
{
"metadata": {
"name": "Arduino"
},
"nbformat": 3,
"nbformat_minor": 0,
"worksheets": [
{
"cells": [
{
"cell_type": "heading",
"level": 1,
"metadata": {},
"source": [
"Arduino in IPython"
]
},
{
"cell_type": "heading",
"level": 2,
"metadata": {},
"source": [
"Import the Libraries"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"from IPython.display import clear_output\n",
"from time import sleep\n",
"import serial\n",
"import os\n",
"import sys"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 1
},
{
"cell_type": "heading",
"level": 2,
"metadata": {},
"source": [
"Run this prior to plugging in your Arduino to set a baseline."
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"os.system(\"ls /dev/tty* > prelist.txt\") # Build a list of all the currently connected tty devices (For linux)"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "pyout",
"prompt_number": 2,
"text": [
"0"
]
}
],
"prompt_number": 2
},
{
"cell_type": "heading",
"level": 2,
"metadata": {},
"source": [
"Plug in the Arduino now."
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"os.system(\"ls /dev/tty* > postlist.txt\") # Build a list of all connected tty devices that includes the Arduino\n",
"pre_list=[]\n",
"with open('prelist.txt', 'r') as f:\n",
" for line in f:\n",
" pre_list.append(line)\n",
"with open('postlist.txt', 'r') as f:\n",
" for line in f:\n",
" if line not in pre_list:\n",
" print(\"Your device id is {}\".format(line))\n",
" dev = line.strip('\\n') # Remove the end of line character"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"Your device id is /dev/ttyUSB0\n",
"\n"
]
}
],
"prompt_number": 3
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"###Copied code from [instructables.com](http://www.instructables.com/id/Interface-Python-and-Arduino-with-pySerial/step3/Program-Idle/) works with Python 2.7\n",
"\n",
" from time import sleep\n",
" import serial\n",
" ser = serial.Serial('/dev/tty.usbmodem1d11', 9600) # Establish the connection on a specific port\n",
" counter = 32 # Below 32 everything in ASCII is gibberish\n",
" while True:\n",
" counter +=1\n",
" ser.write(str(chr(counter))) # Convert the decimal number to ASCII then send it to the Arduino\n",
" print ser.readline() # Read the newest output from the Arduino\n",
" sleep(.1) # Delay for one tenth of a second\n",
" if counter == 255:\n",
" counter = 32"
]
},
{
"cell_type": "heading",
"level": 2,
"metadata": {},
"source": [
"IPython3 Version (Slightly embellished)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"* The code was slightly embellished with the use of clear_output(), and stdout.flush() found in the \n",
"animation example in the official IPython Notebook [Example Collection](https://github.com/ipython/ipython/tree/master/examples/notebooks)."
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"ser = serial.Serial(dev, 9600, timeout = 0.25) # Establish the connection on a specific port\n",
"counter = 32 # Below 32 everything in ASCII is gibberish\n",
"sleep(5) # The Arduino needs 5 seconds to wake up unless the code is uploaded as hex.\n",
"back = ser.readline().decode('ascii') # Python3.* requires explicit encoding and decoding of bytes\n",
"print(back) # Should say 'Ready'\n",
"sys.stdout.flush() # This forces the output to the screen (avoid delays)\n",
"sleep(0.5) # 0.5 second delay\n",
"ser.write(b'\\x00') # Send a \"flush serial\" signal to the arduino (see Arduino Code)\n",
"ser.flush() \n",
"while counter<65: # Send and display the ASCII characters from #32 to #65\n",
" counter +=1\n",
" out = str(chr(counter)).encode('ascii') # The output must be explicitly encoded in Python3.*\n",
" clear_output() # Erase what is currently displayed (avoids a long line of output)\n",
" \n",
" # The clear_output() does cause a flicker, (the lower cells temporarialy jump up).\n",
" \n",
" print(\"{} was sent as {} (ASCII - {})\".format(out.decode('ascii'),out, counter))\n",
" ser.write(out)\n",
" sleep(0.1) # Give the arduino a chance to talk back\n",
" back = ser.readline().decode('ascii')\n",
" print(back) # Read the newest output from the Arduino\n",
" \n",
" sys.stdout.flush() # Reduces the flicker\n",
" \n",
" sleep(0.5) # Delay for 0.5 seconds\n",
" "
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"A was sent as b'A' (ASCII - 65)\n",
"The arduino recieved 'A'\r\n"
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n"
]
}
],
"prompt_number": 4
},
{
"cell_type": "heading",
"level": 2,
"metadata": {},
"source": [
"Arduino Code"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"###Previosly Uploaded Arduino Code\n",
"\n",
"This code needs to be uploaded to the Arduino prior to running this Notebook.\n",
"\n",
" void setup() {\n",
" Serial.begin(9600); // set the baud rate\n",
" Serial.println(\"Ready\"); // print \"Ready\" once\n",
" }\n",
" void loop() {\n",
" char inByte = ' ';\n",
" if(Serial.available()){ // only send data back if data has been sent\n",
" char inByte = Serial.read(); // read the incoming data\n",
" if (inByte != 0){\n",
" char echo[] = \"The arduino recieved '#'\";\n",
" echo[22]=inByte; // replace the '#' with the character recieved\n",
" Serial.println(echo); // send the data back in a new line so that it is not all one long line\n",
" }\n",
" else{\n",
" Serial.flush(); // a call to flush the serial buffer was made\n",
" }\n",
" }\n",
" delay(100); // delay for 1/10 of a second\n",
" }\n",
"\n",
"This is slighly modified from what can be found at [instructables.com](http://www.instructables.com/id/Interface-Python-and-Arduino-with-pySerial/step2/Program-the-Arduino/)."
]
}
],
"metadata": {}
}
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment