Control Arduino Using PHP




I have recently seen a lot of problems regarding PHP and Arduino. Many people don't know about facts like that an arduino needs a 2 second (!!!) delay before it can receive any messages through serial. There are a few other facts you need to pay attention to, I will be going over them in this instructable.

Step 1: Install a -AMP Webserver

It is important you have a webserver with PHP 5.3 or higher. My recommendation is to use LAMP for linux, WAMP for windows and XAMP for osx (though I don't know if it works on osx).

To install LAMP on debian (Ubuntu, turnkey, RPI, etc.) you want to execute the following command:

sudo apt-get install lamp-server^ Don't forget the ^ here!

On windows you just have to go to

Check if the webserver is working by going to "localhost". If not:
You might have skype installed, check its documentation on how to disable it on port 80

Step 2: (Debian Only) Add Permissions for Serial Port

By executing the following command, you grant PHP to use the serial port (USB)

If you are not running Linux you don't have to run this command, in fact, you can't.

For other linux distributions you are going to have to check its documentation (same for osx).

usermod -a -G dialout www-data

(might be executed as sudo)

Step 3: Get PhpSerial

Now, you are going to have to download PhpSerial, which is a cross-platform solution for controlling serial ports. We will be using this to send our messages to our arduino.

Go to and download zip (on the right side)

Extract the zip and get copy the file src/PhpSerial.php to your %SERVERROOT%/www/ directory

Step 4: Finally! Write Your Arduino Code

Now you are going to have to write some code for your arduino. I have an example below:

#include "USBSerial_main.h"

int incomingByte = 0; int pin = 2;

void setup() {

Serial.begin(9600); //Starts the serial connection

pinMode(2, OUTPUT); //Sets pin 2 to be output


void loop() {

if(Serial.available() > 0){; /Removes the message from the serial cache

digitalWrite(2, true); //Enables the led on pin 2

delay(100); //Waits 100 ms

digitalWrite(2, false); //Disables the led on pin 2



Step 5: Creating Your Webpage

I have created an example page that uses the PhpSerial library.

This is easier when you are on unix-based systems, then you can just use fopen (check the php docs on that)

Upload the file with your code to %SERVERROOT%/www and go to localhost.

Here's the code:


$comPort = "/dev/ttyACM0"; //The com port address. This is a debian address

$msg = '';


$serial = new phpSerial;







sleep(2); //Unfortunately this is nessesary, arduino requires a 2 second delay in order to receive the message

$serial->sendMessage("Well hello!");


$msg = "You message has been sent! WOHOO!";





<title>Arduino control</title>



<form method="POST">

<input type="submit" value="Send" name="hi">







    • Trash to Treasure

      Trash to Treasure
    • Arduino Contest 2019

      Arduino Contest 2019
    • Tape Contest

      Tape Contest

    21 Discussions


    10 months ago on Step 5

    for testing, i copied exactly your codes,,php page appears nicely but when i click send button nothing happen,..led is not blinking.


    3 years ago on Introduction

    Thanks for the tutorial, but it returns some errors:

    Warning: Specified serial port is not valid in C:\xampp\htdocs\www\PhpSerial.php on line 120

    Warning: Unable to set the baud rate : the device is either not set or opened in C:\xampp\htdocs\www\PhpSerial.php on line 221

    Warning: Unable to set parity : the device is either not set or opened in C:\xampp\htdocs\www\PhpSerial.php on line 289

    Warning: Unable to set length of a character : the device is either not set or opened in C:\xampp\htdocs\www\PhpSerial.php on line 342

    Warning: Unable to set the length of a stop bit : the device is either not set or opened in C:\xampp\htdocs\www\PhpSerial.php on line 395

    Warning: The device must be set before to be open in C:\xampp\htdocs\www\PhpSerial.php on line 149

    Warning: Device must be opened in C:\xampp\htdocs\www\PhpSerial.php on line 661

    How could i fix it?

    6 replies

    Reply 2 years ago

    You have to give persmission to use the COM port:

    sudo chmod 666 ttyACM0 (where ttyACM0 should be the right address).


    Reply 3 years ago

    this means that either the arduino is not connected or the port name is wrong ($comPort variable) try to list all files in /var/ while the arduino is plugged in and out. then see which appears when arduino is plugged in and use that as the com port address


    Reply 3 years ago on Introduction

    Thanks, maybe it doesn't work properly on windows because (i think) i wrote the port name correctly; but i'll better try it on linux and see what happens


    Reply 3 years ago

    I would love to help now, I do have some code that *should* work on Windows but I currently don't have access to that, or even an arduino...


    Reply 2 years ago

    The USBSerial_main.h is the header for this sketch, it can be left out.


    3 years ago

    Where is "USBSerial_main.h" from?

    When PHP opens the serial connection the Arduino reboots. Every. Single. Page hit.

    I solved that problem with two Arduinos:


    Hey people I am about to embark on a project using this software. Design objective : create a browser screen logged into Pi running a robot. The Pi and PHP will be used to replace the Infrared controller as an easy-control-screen via ERX. Will keep you informed


    Reply 3 years ago

    hey! nice to see this. good luck!

    may I suggest using another language, like python for the backend. or use php which then calls something like python. That way, you are also able to process inputs from the robot and respond to it without user intervention.


    Reply 3 years ago

    it is not a good idea to read data using PHP because of large delays, you should then move to some other language like python.


    Reply 3 years ago

    Its a PHP based project. What should I do using python? Read the data to a text file and then read using php?


    Reply 3 years ago

    PHP is not meant to be reading on interrupt. You should use python or some other realtime language to store the responses and read them in using PHP on a later time, on web request or maybe even trigger a CRON/cli and notify the client to do a new web request.