Realbasic Serial Communication With Arduino
- Serial Communication: Serial communication is a communication process wherein data transfer occurs by transmitting data one bit at a time in sequential order over a computer bus or a communication channel. It is the most widely used approach to transfer information between data processing equipment and peripherals.
- Serial communication on pins TX/RX uses TTL logic levels (5V or 3.3V depending on the board). Don’t connect these pins directly to an RS232 serial port; they operate at +/- 12V and can damage your Arduino board.
These Arduino projects are designed to display the value of inputs using the serialmonitor. Serial is a method of communication between a peripheral and a computer. In this case, it is serial communication over Universal Serial Bus (USB). When reading sensors with an Arduino, the values are sent over this connection and can be monitored or interpreted on your computer.
The DigitalReadSerial Sketch
In this tutorial, we will learn about RS-485 Serial communication between two Arduinos and then demonstrate it by controlling the brightness of the LED connected to a Slave Arduino from Master Arduino by sending ADC values through RS-485 Module. A 10k potentiometer is used to vary the ADC values at Master Arduino.
In the DigitalReadSerial project you monitor the HIGH and LOW values of a button over the serial monitor.
For this project, you need:
An Arduino Uno
A breadboard
A 10k ohm resistor
A pushbutton
Jump wires
Complete the circuit and upload the code from File→Examples→01.Basics→DigitalReadSerial.
After you upload the sketch, click the serial monitor button on the top right of the Arduino window. Clicking this button opens the serial monitor window and displays any values being sent to the currently selected serial port.
In the window, you should see a cascade of 0 values. Press the button a few times, and you should see some 1 values appear.
If you don’t see anything, or you see incorrect values, double-check your wiring:
Make sure that you’re using the correct pin number for your button.
If the jump wires or components are not connected using the correct rows in the breadboard, they will not work.
If you are receiving strange characters instead of 0s and 1s, check the baud rate in the serial monitor; if it is not set to 9600, use the drop-down menu to select that rate.
The AnalogInOutSerial Sketch
Archive and install_failed_invalid_apk showbox. In this project, you monitor an analog value sent by a variable resistor over the serial monitor. These variable resistors are the same as the volume control knobs on your stereo.
In this example, you monitor the value as detected by your Arduino and display it on your screen in the serial monitor, giving you a greater understanding of the range of values and performance of this analog sensor.
You need:
An Arduino Uno
A breadboard
A 10k ohm variable resistor
A resistor (greater than 120 ohm)
An LED
Jump wires
The circuit uses an LED connected to pin 9 as in the Fade circuit. The code fades the LED on and off according to the turn of the potentiometer.
Because the input and the output have a different range of values, the sketch needs to include a conversion to use the potentiometer to fade the LED. This is a great example of using the serial monitor for debugging and displays both the input and output values for maximum clarity.
Arduino Serial Communication Tutorial
Complete the circuit and upload the code from File→Examples→03.Analog→AnalogInOutSerial.
After you upload the sketch, turn the potentiometer with your fingers. The result should be an LED that fades on and off depending on the value of the potentiometer. Now click the serial monitor button on the top right of the Arduino window to monitor the same values that you are receiving and sending to the LED.
If you don’t see anything happening, double-check your wiring:
Make sure that you’re using the correct pin number for your variable resistor.
Check that your LED is the correct way round, with the long leg connected to Pin 9 and the short leg in GND, via a resistor.
If the jump wires or components are not connected using the correct rows in the breadboard, they will not work.
If you are receiving strange characters instead of words and numbers, check the baud rate in the serial monitor. If it is not set to 9600, use the drop-down menu to select that rate.
Serial communications can be used in many ways, but in this situation we will use it to turn off and on an LED.
Just add an LED to pin 13 and ground. Then connect your USB cable.
Create an integer to store the incoming serial byte. I decided to name it 'inByte'
In the void setup, set pin 13 as an output and use 'Serial.begin(< baud rate >);' to initiate the serial communication. We used 9600bits per second, so we added - Serial.begin(9600);
in the void loop we first need to know when a byte is available to be read. So, we use 'Serial.available()' This function returns the number of bytes that are available to be read. So, if we use 'if(Serial.available() > 0)', we know there is at least one byte that we can read.
inside that if statement, we need to read the byte and store it for later use. we will use 'Serial.read()', which will read the byte. then we will save it to inByte. In total, this line read: inByte = Serial.read();
While still being inside the main if statement (<'if(Serial.available() > 0)>) we add if statements. To read or write a letter to a variable, we will put the letter in single quotation marks. We wanted our LED to turn on if we sent an 'a' to the Arduino. Thus, our if statement read: 'if(inByte 'a')' inside the if statement we added set the pin to HIGH.
Also, we used 'Serial.println()' so that the arduino will notify us, right within the Serial monitor, that it has read an 'a' and has turned on the LED.
This if statement looked like this:
if(inByte 'a') { // byte is 'a'
digitalWrite(13, HIGH);
Serial.println('LED - On');
}
We also wanted the LED to turn off if any other letter was read. so we added an else statement which turned the LED off.
This all may seem confusing, but it is easier to understand if you look at the final code on the next page.