Let’s try to evoke the difference between a digital and an analog
signals.
The analog signal was examined at the beginning of Week 2.
When we started the visualizer, we saw
the signal change swiftly: the value was changing from 0 to 5 V.
The digital signal can either be present or absent:
it will either take the value of 0 or 1.
In Arduino pin terminology, this is either High or Low.
This means that when we record a digital signal, we write digitalWrite (high)
and we put the value of 5 V at the specified output.
And when we write digitalWrite (low), we set the value of 0 V at the output.
Now we are going to learn how to read off digital signals:
we will also try to determine whether or not there is voltage
at the input.
While reading off the signal, the controller will be determining
whether current voltage is higher or lower than 3 V.
Thus, the “low” or 0 value will be assigned to anything lower than
3 V, while
the “high” or 1 value will be given to voltage higher than 3 V.
Now we are going to prepare a sketch to experiment with.
In this experiment, we will be transmitting everything that
is passed to
input 7 directly to the in-built LED and output 13.
To make an input and an output out of the 7th and 13th pins respectively,
we shall start with the pin mode function designation in setup.
For LED_PIN, pin 13, this will be “output”,
and for DIGITAL_IN, pin 7, this will be “input”.
Our loop will consist of recording the digital signal
on LED_PIN leaving it in the same state that our DIGITAL_IN pin is now.
Now we need to receive this digital signal at input 7.
For this purpose, we shall assemble a simple circuit.
A simple tact switch will help us receive this signal at input 7,
the same switch we used
during Week 1 to manually control a traffic light.
At one end, I have connected it to the 5 V Arduino output with this
red wire,
and at the other end, I have used this white wire to connect the switch
to input 7.
Consequently, when the tact switch is on, the 5 V voltage will be transmitted
to input 7.
We will determine high voltage level and will transmit the signal to
output 13.
Let’s try and see if this works.
Yes, it really does work.
I am now going to say a couple of words about the resistor which
I connected at the same end
that has a wire connection with Arduino.
This is the so-called pull-up resistor.
It is connected to the “ground” link on the breadboard which, in its turn,
is connected to Arduino’s “ground”
link.
We need the resistor in order to prevent the switch from picking up any noise
that always exists on air.
I can remove it from the circuit, and we shall see
that the LED is on when the switch is off.
When I press it on, it will be brighter, but during the rest of the time,
input 7 will be receiving voltage which
is perceived by the controller as high, so the LED will also be on.
Now let’s look at the sketch again: I am suggesting an alternative
to get rid of the noise on air.
We shall configure the output destined for the switch and marked as DIGITAL_IN,
not into a simple INPUT, but into INPUT_PULLUP.
What is it exactly?
It turns on the pull-up resistor which is built in Arduino,
although in this case the pull-up is performed towards the voltage supply
and not the “ground” link.
Some people separate the terms of “pull-up resistor” and “pull-in
resistor”,
but I’m used to calling it a “pull-up resistor”, as it pulls the switch
up either towards the “ground”
link or the voltage supply.
As a result, the INPUT_PULLUP pulls the switch up towards the voltage
supply.
To make it work, I have set the second output of the switch
from 5 V to “ground”.
And now, look how the LED is on when the switch is off.
Thus, when the switch is off,
high voltage exists at the input because of this INPUT_PULLUP
function.
When we press the switch on, the voltage disappears,
therefore, the switch operates vice versa.
This method is more convenient, in my view,
as it simplifies the scheme. Moreover, we can get rid of all this input noise
by changing only one single parameter.
To conclude, we have learnt to read off a basic digital signal.
Why do I say “basic”?
All digital devices that surround you
communicate with each other in a more intricate manner
than just through “yes” or “no”, “signal” or “no signal”.
They use pre-agreed communications protocols,
where it is discussed and agreed in advance what the combination of
high and low voltage levels in certain periods of time signifies.
We will later have to deal with sensors
which use complex protocols, but this will happen over the course of the
upcoming weeks.