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

Arduino Projects 2: Analog and Digital Signals

DFRobot Jun 21 2017 965


Let's begin!

Let’s draw an analogy between microcontrollers and people

The microcontroller is the “brain”.

Code is the microcontroller’s “mind”.

Hardware (sensors and actuators) are the microcontroller’s “body”.

Signals, such as analog and digital signals, are the microcontroller’s “nerve impulses”.


Digital Signals

We will use a digital button module to demonstrate a digital signal. This is included in Gravity: Stater Kit for Arduino kit.

Parts Needed:

DFRduino UNO (similar as Arduino UNO R3) x1
I/O Sensor Expansion Shields V7.1 x1

Digital Push Button Module x1

Connections

Take the Sensor Expansion Shield and stack it on top of your microcontroller – make sure that the pins are correctly aligned to avoid damage to the shield.
See the diagram below for further details:

Take the Digital Push Button module and connect it to Digital Pin 2 (be sure that the power, ground and signal connections are correct or you risk damaging your components!)

Fig. 2-1 Digital Pin Connection


When the connections are made, connect the USB cable. This will power on the microcontroller. We can now prepare our program to upload.

Serial Port Monitoring

Open Arduino IDE and select: File > Examples > 01.Basics > DigitalReadSerialThe 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”. Arduino IDE will verify the code and then upload it to your microcontroller.

Once the upload is complete, open the serial monitor. To do this, navigate to Tools > Serial Monitor, or click the magnifying glass icon on the top right hand corner of the toolbar.

The serial monitor shows information from the arduino microcontroller. 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 off (not being pressed).

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

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


Analog Signal

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

Parts Needed:

DFRduino Uno   x1
I/O Sensor Expansion Shield V7.1  x1
Analog Rotation Sensor  x1 


Connections

Connect the rotation sensor to the DFRduino UNO (the same as Arduino UNO) Analog Pin 0. Make sure that signal, ground and power are connected to the correct corresponding pins, just like in the previous session. Connect the USB cable to the arduino microcontroller to power it on. We can now upload a program.

Fig. 2-2 Analog Pin Connection


Code Input

In Arduino IDE, navigate to File > Examples > 01.Basics > AnalogReadSerial
A new window with a program will appear. It should read as follows:
void setup() {                               //Initial setup
Serial.begin(9600);                    //Set 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”. Arduino IDE will verify the code and then upload it to the microcontroller.

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 baud.

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 a full rotation.

The Difference Between Digital Pins And Analog Pins

Serial Port Monitor

We use the Serial Monitor to view information from the microcontroller. We can also use it to send information back to the microcontroller, and interact in with it in various ways while it runs a program.

   


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 Gravity: Arduino starter 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!

There is also another arduino starter kit for you to choose.

REVIEW