Thursday 11 October 2018

Arduino - III

In the last post, we saw the serial monitor output from Arduino. In this post we will take a look at how we can communicate with Arduino by using serial input. We will continue to use the same Arduino Uno and Arduino IDE used in the last post.

The first program that we will see is shown below:

// read from serial input and post it to serial output using println

void setup() {
  Serial.begin(9600);  // initialize serial port
}

void loop() {
  
  if (Serial.available() > 0) {    //Serial.available() returns the number of bytes (characters) available                                                     //for reading from the serial port
    int inByte = Serial.read();    //Reads incoming serial data as bytes
    Serial.println(inByte);        //Prints data to serial monitor in human-readable ASCII text
  }
  
}

Once this program is uploaded onto Arduino board, we can see the outputs for the inputs shown below:

1) Clicking just Send button with no input returns

10

2) Input 0 returns

48
10

3) Input 1 returns

49
10

4) Input Arduino Uno returns

65
114
100
117
105
110
111
32
85
110
111
10

The outputs are shown below:



















10 is returned after every input as it is newline character. To turn it off in output, we can set the No line ending in the first dropdown in right bottom of Serial monitor from the default value of Newline. Also, the output is the ASCII representation on the input characters. Note the 32 in the last output that stands for space character in between Arduino and Uno.

In the next program we look at a similar program but we use write instead of println();

// read from serial input and post it to serial output

void setup() {
  Serial.begin(9600);  // initialize serial port
}

void loop() {
  
  if (Serial.available() > 0) {    //Serial.available() returns the number of bytes (characters) available                                                     //for reading from the serial port
    int inByte = Serial.read();    //Reads incoming serial data as bytes
    Serial.write(inByte);          //Writes binary data to serial monitor
  }
  
}

Passing the same inputs as in above case, we get below output:

1) Clicking just Send button with no input returns nothing

2) Input 0 returns

0

3) Input 1 returns

1

4) Input Arduino Uno returns

Arduino Uno

The outputs are shown below:



















In the last example, we will control a LED by entering an input in Serial monitor:

// read from serial input and control a LED

int red = 10;  // initialize the number of red pin
int inByte;

void setup() {
  Serial.begin(9600);  // initialize serial port
  // initialize digital pin red as an output.
  pinMode(red, OUTPUT);
}

void loop() {

  if (Serial.available() > 0) {    //Serial.available() returns the number of bytes (characters) available                                                     // for reading from the serial port
    inByte = Serial.read();        //Reads incoming serial data as bytes
  }
  if (inByte == '1') {
    digitalWrite(red, HIGH);     //Input of 1 will power on LED
  }
  if (inByte == '0')
  {
    digitalWrite(red, LOW);     //Input of 0 will power off LED
  }
}

The program in IDE is shown below:














The circuit is shown below:




















Once the program is uploaded, sending an input of 1 in Serial monitor will fire up the LED as shown below:




















Sending an input of 0 will power off the LED.

With this example, we conclude this post on Arduino.