$USD
  • EUR€
  • £GBP
  • $USD
TUTORIALS Arduino

Arduino Intermediate Kit Tutorial 3: Learn Digital and Analog Signals through Serial Port

DFRobot Aug 31 2017 634

Let’s review our previous session.

We can draw an analogy between Arduino products and human beings:

The Arduino functions as a “Brain”.
Code is the Arduino’s “mind
Hardware, such as sensors and actuators, are the Arduino’s “body”.
Signals, such as analog and digital signals, are the Arduino’s “nerve impulses”.

In this session, we will explore the differences between analog and digital signals in more detail.

Digital Signal

We will use a digital button module to demonstrate digital signal. This is included in your kit.

Materials Needed: 

1×  DFRduino UNO R3 (with corresponding USB cable)
1×  I/O Sensor Extension Board V7.1
1×  Digital Push Button Module

HARDWARE CONNECTIONS
Take the Sensor Extension Shield and stack it on top of the Arduino – make sure that the pins are correctly aligned.

Take the Digital Push Button module and connect it to digital pin 2. Be sure that the power, ground and signal connections are the correct or you risk damaging your components. See the diagram below for further details:

Fig. 2-1 Digital Pin Connection

When the connections are done, attach the USB cable. This will power on the Arduino. We can now prepare our program to upload.

SERIAL PORT MONITORING
Open Arduino IDE and select: “File” > “Examples” > “01.Basics” > “DigitalReadSerial”. The following code should appear:

int pushButton = 2;             //connect to digital pin 2
void setup() {                   // initial function
  Serial.begin(9600);            // set up baud rate of the serial port
  pinMode(pushButton, INPUT);    // set the button to be in the output mode
}
void loop() {                              //main function
  int buttonState = digitalRead(pushButton);     // record statistics about the status of digital pin 2
                                           
  Serial.println(buttonState);              // Serial port print statistics about the status of digital pin 2
  delay(1);                             // delay 1ms
}


Click “Upload”. The Arduino IDE will verify the code and then upload it to your Arduino.

Once the upload is complete, open the serial monitor. To do this, navigate to “Tools” > “Serial Monitor”


The baud rate is the data speed that the serial port communicates. It needs to be set to 9600 baud.


The serial monitor shows information from the Arduino board. We are going to use it to view the status of the button. You should see “0” appearing continuously. This indicates that the button is not being pressed (off).

If you press the button, you will see “1” appear for as long as the button is pressed. This indicates that the button is being pressed (on).

“0” and “1” or off and on are the two values of digital signal. We can use this in various ways with our Arduino inventions.

ANALOG SIGNAL

In this section we are going to investigate analog signal in more detail. We are going to use a sensor which uses analog values: the rotation sensor.

Materials Needed:

1×  DFRduino UNO R3 (with corresponding USB cable)

1×  I/O Sensor Extension Board V7.1

1×  Rotation Sensor-L

We will use the Arduino UNO and I/O extension board for all of our experiments. To save time we will omit them from the materials needed section.

HARDWARE CONNECTIONS

Connect the rotation sensor to analog pin 0. Make sure that signal, ground and power are connected to the correct corresponding pins, just like in the previous experiment. Connect the USB cable to the Arduino to power it on. We can now upload code.


Fig. 2-2 Analog Pin Connection

CODE INPUT
In Arduino IDE, navigate to “File” > “Examples” > “01.Basics” > “AnalogReadSerial”
A new window with code will appear. It should read as follows:

void setup() {                     //Initial setup
  Serial.begin(9600);             //Set port baud rate
}
void loop() {                          //Main function
  int sensorValue = analogRead(A0);     // Read status of analog pin 0.
  Serial.println(sensorValue);         // Print status of pin 0 in Serial Monitor
  delay(1);                             //Delay 1ms
}

Click “Upload”. The Arduino IDE will verify the code and then upload it to the Arduino. 

Once the upload is complete, open Arduino IDE’s serial port monitor. To do this, navigate to “Tools” > “Serial Monitor”

Set baud rate of the serial port to be 9600.

A series of values will flash in a column in the serial monitor. 

Try to turn the rotational sensor. A figure between 0 and 1023 will show up, depending on how far the rotational sensor’s shaft is rotated. 0 means no rotation and 1023 means full rotation.
COMPARISON BETWEEN DIGITAL PIN AND ANALOG PIN
1、Serial Port Monitor
We use the Serial Monitor to view statistics from the Arduino. We can also use it to send statistics back to the Arduino, and interact in with it in various ways while it runs a program.
Differences between digital pin and analog pins are clearly shown on the screen of serial port monitor.

2、Code Differences
If you examine the code in detail, you can see that digital pins and analog pins are read using different commands:

To read the value of a digital pin, we use the command: “digitalRead()”
To read the value of an analog pin, we use the command: “analogRead()”

Digital Pins:
int buttonState = digitalRead(pushButton);
// read values from digital pin2

Analog Pins:
int sensorValue = analogRead(A0);
//read values from analog pin0

This will be explained in more detail in the next few sessions.

Try it yourself
The sensors in your DFRobot kit connect in much the same way as the ones demonstrated. Just make sure your power, ground and signal connections are correct. Once connected, you can check the serial monitor for output and see what values they output. Try to experiment and have fun!


If you have any questions, please feel free to comment.
There is also another arduino starter kit for you to choose.

Related category: arduino kits > education kits
Arduino Intermediate Kit Tutorial 2: What Makes Our Devices “Alive”?
Next Arduino Tutorial: Arduino Intermediate Kit Tutorial 4: Make an LED blink


REVIEW