Communicating serially with the Arduino is really easy. Here, we'll show how to take some temperature measurements and then send the measurements over the serial link.
Unlike other devices, Arduino serial communications is simple to use. The Arduino programming environment has a feature, called the serial monitor, which is specifically for viewing the serial data communication. To activate it:
- Go to toolbar
Here are some Arduino serial communications functions.
This function sets the serial communications speed. It has one parameter, speed, which is usually set to 9600.
This function receives data from the serial port.
This function sends data via the serial port. The parameter val can be a single variable, a string or an array.
This functions prints val to the Arduino IDE serial monitor using some specific format.
The LM35 is an ideal temperature sensor for measuring ambient temperature. Different versions of the device are shown below.
Connection Diagram
The LM35 provides a linear output proportional to the temperature, with 0 V corresponding to 0 degrees C and an output voltage change of 10 mV for each degree C change. LM35s are easier to use than thermistors and thermocouples because they are so linear and require no signal conditioning.
The output of an LM35 can be connected directly to a Arduino analog input. Because the Arduino analog-to-digital converter (ADC) has a resolution of 1024 bits, and the reference voltage is 5 V, the equation used to calculate the temperature from the ADC value is:
temp = ((5.0 * analogRead(pin)) / 1024) * 100.0
Experiment:
Hardware Required
- 1 x LM35 temperature sensor
- 4 x LEDs
- 2 X 220 Ohm resistors
- 1 x Arduino Mega2560
- 1 x breadboard
- 10 x jumper wires
Circuit Diagram
Connect the components as shown in the circuit diagram above:
The sensor will be powered by the 5V and GND pins from the Arduino. The sensor output connects to Arduino pin A0, and when the code reads the value of this analog voltage using the function analogRead(0), it returns a value from 0 to 1023. The program will then calculate the temperature using the formula:
temp = ((5.0 * analogRead(pin)) / 1024) * 100.0
or
temp = analogRead(pin) * 0.48828125;
Finally, the temperature reading will be written to the IDE serial monitor with the function Serial.print().
Toni
2017/1/24 19:19:19
You are a genuis!