so please read this whole thing.

My Project: I am making a bell system that is based on PHP. The bell is connected to the arduino UNO via a relay and the arduino is connected to the computer via a USB cable. The interface with the database is built with PHP and MySql. Then we have the code which checks every minuit if the bell has to be rung and if it does send a numeric value to the arduino. There is also an option for a longer bell if we send a '0' to the arduino.

My Code: So this is the file that checks if bell has to rung and sends value to arduino

$usb_comPort = "COM3";
$time = date("H:i");
$time = trim($time);
$sql = "SELECT bell_amount FROM bell_db WHERE DATE_FORMAT(bell_time,'%H:%i') LIKE '$time'";
$result = mysqli_query($conn, $sql);
$numRows = mysqli_num_rows($result);
if ($numRows == 0) {
  echo 'Error: Nothing found!';
} else {
  $row = mysqli_fetch_assoc($result);
  $bellAmount = $row['bell_amount'];
  $runCommand = "ECHO " . $bellAmount . " > $usb_comPort";
  echo $runCommand;
<meta http-equiv="refresh" content="60">

and this is the arduino's code:

int b = 0;
void setup() {
pinMode(10, OUTPUT); //relay

 void loop() {
  b = Serial.read();

    b = b - 48;
    if(b == 0){

    for (int i=0; i < b; i++){
      digitalWrite(10, HIGH);
      digitalWrite(10, LOW);



The Issue that i am facing: My aim is that the code should run automatically when the computer is turned on. The code works fine but there is random bug. The code is uploaded to the arduino. When the computer is turned on i want that the system should work but when i run the file (the one that checks if there is a bell to be rung or not ) it processes the code but it is not able to send a value to the com port so the bell does not ring(the serial light on the arduino does not flash). But if i go to the arduino ide and send a value through the serial monitor once then run the php file, it works!

I am running this on a windows 10 computer, i have an arduino UNO and i have tried this with 2 arduino's same result on both.

So anyone know what i can do?

  • 2
    To make your question easier to read and answer you should remove all the database- and time-related stuff from your PHP code and concentrate only on the communications with Arduino. This forum isn't the place to be debugging non-Arduino code. I suggest you create a function that tells the Arduino to ring the bell. You debug that by calling it directly and post that function here if you have problems with it. You can simply print out what you would pass to the function when debugging your database and other code.
    – cjs
    Commented Apr 2, 2017 at 14:10
  • You need to look at Serial.available() rather than blocking on Serial.read(). Also what happens if your code receives a space? b = b -48 = -16 and then for (int i = 0; i < -16; i++) it isn't going top work how you expect. Commented Apr 3, 2017 at 7:36
  • @Matt I have made sure that white space and alphabets are not sent and even if blank is sent nothing happens.
    – Dhruva
    Commented Apr 3, 2017 at 16:39

1 Answer 1


Sounds like the COM port is not setup. Opening the Arduino IDE probably does this automatically.

Try running the following command, only once, after turning on your pc to initialize the COM port.

mode COM3 BAUD=9600 PARITY=n DATA=8

Ps. I'm not a windows user, so this is an educated guess - reference.

Pps. I think you're winning the award for the most complicated way to ring a bell. :)

  • Thanks dude.. well do you have any other better methods of doing this?
    – Dhruva
    Commented Apr 3, 2017 at 16:40
  • Well I'm not exactly sure what your requirements are, I'll assume is to ring the bell based on information in the database. In that case you could eliminate the webpage and browser all together and write a command line application/daemon/service. You could still write it in php if you wanted, and it would be more accurate and less error prone than relying on the meta/refresh in a browser.
    – Mazaryk
    Commented Apr 3, 2017 at 16:49

Not the answer you're looking for? Browse other questions tagged or ask your own question.