Introduction: Interface Arduino to MySQL Using Python

Picture of Interface Arduino to MySQL Using Python

Here's a brief tutorial that should get you up and running interfacing your Adruino with a MySQL database. For the sake of this tutorial, I am assuming you know how to set up and use MySQL. This tutorial does not require much Python experience, but you will be required to install two Python libraries for this project.

Glad we're on the same page, let's get to it!

Step 1: Downloading and Installing the Python Libraries

Picture of Downloading and Installing the Python Libraries

First I'll point you in the right direction for installing the required Python libraries. First you'll need to install the pySerial library. Simply put, the pySerial library allows your Python script to talk with the serial port in which the Arduino is connected. I.e. you can kind of think of it as a stream connecting the Arduino code to the Python code (insert other silly analogies here).

1. You can download the pySerial library here:

2. For mac or linux users, download the file and extract it. Open terminal and cd into the extracted folder and run the following command:

python install

This will install the pySerial package. (screen shot below)

Next, we will install the library to allow Python to talk with MySQL called MySQLdb.
I just want to note, this step can be very annoying, but very rewarding once completed. I have included a guide for you to follow, but I recommend you have MySQL, python, and XCode(or the latest GCC) installed before you try and install MySQLdb.

1. download the library from source forge:

2. If you're lucky enough, you should just be able to download it, extract it, open Terminal, cd into the folder and run python install, just as you did before. If this works, you're awesome and you should awesome, but if not, this guide should help. Note, I had to do step 6 before step 3.

Step 2: Fewf, Now Let's Set Up Our Arduino!

Picture of Fewf, Now Let's Set Up Our Arduino!

All right. Now that we've gotten all of the annoying steps out of the way, let's get to the fun parts!

For the sake of getting you up and running, I'll keep this short and concise.

1. Let's get our Arduino sending some output.

What we're going to do is essentially send data from our Arduino for our Python code to process, so let's first get our Arduino to send some data.

I have a temperature/humidity sensor lying around, so I'm going to take the readings from this and send them to my Python code.

Here's the sample code:

//you can ignore this part, just for the temperature sensor
#include "DHT.h"
#define DHTPIN 2
#define DHTTYPE DHT22

void setup() {
  dht.begin(); //start the temp reading (agian only for temperature sensor

void loop() {
  //read the temperature and humidity (temperature sensor specific code)
  float h = dht.readHumidity(); //read humidity
  float t = dht.readTemperature(); //read temperature (C)
  // check if returns are valid
  if (isnan(t) || isnan(h)) {
    Serial.println("Failed to read from DHT");
  } else {  //if it read correctly
    Serial.print(h);     //humidity
    Serial.print(" \t"); //tab
    Serial.println(t);   //temperature (C)

It should be pretty straight forward. Again, I'm using a temperature/humidity sensor to get some data to send to the Python, but this could obviously be substituted with anything other data; it's just used as an example!

Note: the Serial.print lines are the data that is being sent to the serial port that the Python code will be grabbing and doing all the wonderful things with it.

Step 3: Let's Go Ahead and Set Up Our MySQL

Picture of Let's Go Ahead and Set Up Our MySQL

Now that we have the code running on our Arduino, we need some Python code to talk to it, but first we need a MySQL database and table to store this data.

Our Arduino is reading the temp/humidity data every second and writing it with Serial.print(). So we're going to write some Python  to grab this data and insert it into some MySQL.

First, I'll create a simple MySQL table to store this data.

create table weatherData (
weatherDataID int(11) AUTO_INCREMENT NOT NULL,
humidity decimal(4,2) NOT NULL,
tempC decimal(4,2) NOT NULL,
constraint weatherData_PK primary key (weatherDataID)

This table is simple enough, just going to store the humidity and temperature reading that I'm getting from the Arduino.

Attached is a screen shot of me setting up this database using the mysql command line. Here's a wonderful guide to refresh your memory on the process if need be (I know I reference it monthly).

Step 4: Python TIEM

Picture of Python TIEM

Alright, fewf, now we've got our Arduino ready and a database all prepared for our data. Last step is to write the Python to get this data and insert it into our database.


import serial 
import MySQLdb

#establish connection to MySQL. You'll have to change this for your database.
dbConn = MySQLdb.connect("localhost","database_username","password","database_name") or die ("could not connect to database")
#open a cursor to the database
cursor = dbConn.cursor()

device = '/dev/tty.usbmodem1411' #this will have to be changed to the serial port you are using
  print "Trying...",device 
  arduino = serial.Serial(device, 9600) 
  print "Failed to connect on",device    
  data = arduino.readline()  #read the data from the arduino
  pieces = data.split("\t")  #split the data by the tab
  #Here we are going to insert the data into the Database
    cursor.execute("INSERT INTO weatherData (humidity,tempC) VALUES (%s,%s)", (pieces[0],pieces[1]))
    dbConn.commit() #commit the insert
    cursor.close()  #close the cursor
  except MySQLdb.IntegrityError:
    print "failed to insert data"
    cursor.close()  #close just incase it failed
  print "Failed to get data from Arduino!"

Okay, so hopefully this is relatively understandable from the comments. The real important parts to note are to make sure you configure the connection to be specific to your data for your database (i.e. username/password/database name). Secondly, you're going to want to change the device='' line to point to the usb serial port that you are using.

Once you configure this script as you needed, you should see the data being populated in your MySQL table when you run the script. Here's an example below of what mine is populating like (see image).

Well that's about it! Hopefully you're all set up an good to go now. You should be able to do a number of cool things now with this basis, and I hope you have some fun with it. Go put this data on your website or do whatever your heart desires!

Thanks for reading, and please please please feel free to let me know if you have any suggestions to improve this tutorial, or have suggestions for any tutorials you'd like to see in the future.



jamcry (author)2017-10-22

Hello, thanks for this helpful tutorial. Is it possible to reverse this process? What I mean is can arduino read data from the database ?

russ_hensel (author)2016-01-23

I love the combination of Python and the Arduino. So I have created a collection about it. I have added your instructable, you can see the collection at: >>

falexis19 (author)2016-01-05


armorer243 (author)2014-04-07

A little help, if you would be so kind. I am a python noob so please bear with me. This script seems to insert the data once and then exit. Is there a way for this to run as a loop over and over, inserting data every time it receives a new string?

mangopeach (author)armorer2432014-04-07

Hey there,

I haven't testing this, so just a warning, but a straightforward solution would be to put the the insertion in a loop and do an insert each time some data was read. Something along the lines of (just restart the loop if you don't have any input, so you don't try and insert null values into your database):

while True:


data = arduino.readline() #read the data from the arduino

if not data.strip(): #did we get an empty line?

#could put a sleep(1) or something if you wanted to wait before trying to read again

continue #start the loop over

pieces = data.split("\t") #split the data by the tab

#Here we are going to insert the data into the Database


cursor.execute("INSERT INTO weatherData (humidity,tempC) VALUES (%s,%s)", (pieces[0],pieces[1]))

dbConn.commit() #commit the insert

cursor.close() #close the cursor

except MySQLdb.IntegrityError:

print "failed to insert data"


cursor.close() #close just incase it failed


print "Failed to get data from Arduino!"

ArdsH (author)mangopeach2015-10-18

your code didnt work for me.. can u check it sir?

hei dude, i just get error in except

Gombakka (author)2015-05-14

I made this one through lot of other referencing .But I have to say this tutorial was my major reference.Now I am finding a way to insert data continuously to my database.This script runs only once.Thank you so very much for sharing :)

Here's a good guide on installing MySQLdb in Kali -Linux

ArdsH (author)Gombakka2015-10-18

@Gombakka please send me an example code on how you manage go get it looping.. i tried but mine didn't work out.. please message here on send to my email thanks

khios78 (author)Gombakka2015-05-27

Did you manage to get it to loop? I'm not sure of the code right now but before it starts the write to database you could add while loop to keep repeating until a condition.. Eg space bar or whatever. I'm going to set a loop that it will never exit from as I need it running In the background to keep inserting data every 5 mins

Gombakka (author)khios782015-05-28

Yes I did,I am sending data every 3 hours to my database. Even though you add a 'while true' loop data insertion to the table will stop at some point unless you open the cursor to the database inside that loop :)

purehektik (author)2015-07-05

Thanks for this tutorial! But you forget to explain the important last step and therefor I am not able to complete this tutorial. In step 4 you just paste the python script. But what should I do with it? It needs somehow to be executed, or not? This isn't explained anywhere :(

khios78 (author)2015-05-27

Worked well. I had to do a little fault finding but that might have been my typing. Just got to make it loop. Now.

Would you mind if I used your code as my base (with the right thanks and links back to you of cause) building a automated hydro setup that I can view from online but rather code it myself as least I know what's in it etc ;)

khios78 (author)khios782015-05-27

Will only work under Python2 as it is now. It can be made to work under python3 by adding print("blah") and that will work with Python2 first (tbh I always use print

Print("") anyway. Habbit

gembong.wijang (author)2015-04-10

umm.. bro, i'm python newbie. how to run the python program?

skolk made it! (author)2014-11-03

Used this as a way to learn some inital mysql and check a different sensor (optical interrupter) . Thanks!

JackCouls (author)2014-09-23

why am i always failed to connect to usb port? i am using windows and arduino uno with serial cable attached to usb port COM3, it always printed that failed to get data from Arduino. as written in the codes on the last except section.

any help would be appreciated


florianagr (author)2014-04-15


your tutorial is great!!!

But i still have a problem.

My Sql database is empty.

I´m getting no error, only a warning:"Warning: Out of range value for column 'tempC' at row 1"

I only use one sensor: temerature sensor (DS18S20).

In my python code i only delete the humidity data


cursor.execute("INSERT INTO weatherData (tempC) VALUE (%s)", (pieces[0]))

dbConn.commit() #commit the insert

cursor.close() #close the cursor

Nothing is happening in my Database.

What can be the reason.

Thanks for helping me

Arizno (author)2014-03-11

There is a site that make the MySqldb install a breeze. Literally a few button clicks.

armorer243 (author)2014-03-11

Very useful! I set up my smoker to send it's data via wireless to my home server. Then using your 'ible it is logging it's data to a db. Last part is my internal webpage with live graphing I can access at work via VPN. I must say your tutorial gave me the exact starting point I needed to finish the project up. Thank you very much!

hard_candy (author)2014-01-14

I have to say, I find this tutorial very cool, but I'm having a couple of issues, hope you can help me.

I did everything you said with the difference that I'm using a RFID card reader to send data. The problem seems to be that my Python script never receives this data. I have tryed with a very simple code to send something from the Arduino to Python and I discovered that it just keeps looking for data (specificaly this line):

data = arduino.readline() #read the data from the arduino

but never goes forward and never prints the excepcion. Everything what is printed in the terminal is:

Trying... /dev/ttyXXX

And that is it. I don't know what is that due to. Could you help me?


hard_candy (author)hard_candy2014-01-15

Ok, I found the problem. This is what I saw in the documentation (

>>> ser = serial.Serial('/dev/ttyS1', 19200, timeout=1)
>>> x = # read one byte
>>> s = # read up to ten bytes (timeout)
>>> line = ser.readline() # read a '\n' terminated line
>>> ser.close()

as one can see, readline reads a '\n' terminated line and I was printing with Serial.print(val); then I changed it for Serial.println(val); and it worked :)



alochmandy (author)2013-12-30

Thank's man. This looks like a real good solution. Can't wait to try it.

breather (author)2013-10-28

I don't understand the following point:
device = '/dev/tty.usbmodem1411' #this will have to be changed to the serial port you are using

I know what's mean /dev/ttyX on Linux but I don't understand in this point.
how can I know my serial port?


sspence (author)2013-04-24

Why not run the mysql connector on the Arduino. No intermediary needed.

breather (author)sspence2013-10-28

That's an other possibility but I'm working with Arduino YUN and all of its possibilities so I'm really interesting on the option of this post.

Also the cost of a Arduino Yun is lower than a Wifi shield + Arduino board.

jmigue9333 (author)sspence2013-10-17

Hey do you know if there is any chance to use a wifiShield instead of EthernetShield?

sspence (author)jmigue93332013-10-17

Sure, no problem. TCP/IP is TCP/IP, no matter if cabled or wifi.

mangopeach (author)sspence2013-04-24

That's very cool! I hadn't come across that yet. That seems like a very good option with an ehternet shield. Thanks for the tip.

jatinbatra (author)2013-07-21

Take a bow :)

Jayefuu (author)2013-04-24

Useful! I'm using Python to interface mysql and a serial connected radio module at the moment.

About This Instructable




Add instructable to: