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

Arduino/Genuino 101 Starter Kit Tutorial - Lesson 2: Digital and Analog Signals

DFRobot May 10 2017 496

Arduino/Genuino 101 Starter Kit Tutorial - Lesson 2: Digital and Analog Signals

After we know the mechanism of different types of signals, in this chapter, we will use the serial monitor in Arduino IDE to find out how they looks like in a real circuit.


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


COMPONMENT LIST:

Genuino/Arduino 101 Board (with A to B USB cable)

I/O Sensor Extension Board V7.1


Digital Push Button Module



HARDWARE CONNECTIONS

Take the Sensor Extension Shield and stack it on top of the 101 – make sure that the pins are correctly aligned then press it all the way down.


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:



When the connections are done, attach the USB cable. This will power on the 101. 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; void setup() { Serial.begin(9600); pinMode(pushButton, INPUT); //connect to digital pin 2 // initial function // set up baud rate of the serial port // set the button to be in the output mode //main function // record statistics about the status of digital pin 2 int buttonState = digitalRead(pushButton); // Serial port print statistics about the status of digital pin 2 Serial.println(buttonState); delay(1); // delay 1ms }

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


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. However, instead of hardware serial port, 101 communicates with PC through a virtual software serial port. You may set the baud any value you want and everything will work just fine.



The serial monitor shows information from the 101 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.


COMPONMENT LIST:

Rotation Sensor-L


We will use the 101 and I/O expansion 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 101 to power it on. We can now upload code.


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() { Serial.begin(9600); //Initial setup //Set port baud rate //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 101.

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 101. We can also use it to send statistics back to the 101, 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 Arduino 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!

REVIEW