- Learn about special pins on the Arduino Uno board
The following special pins impart some unique functionality to the Arduino.
- TX/RX pins
We communicate with the Arduino board using serial communication, as elaborated in the previous posts. The digital pins D0 and D1 facilitate serial communication. If you observe closely, you can see ‘TX’ written next to D1 and ‘RX’ written next to D0. Here, ‘TX’ stands for ‘Transmission’ and ‘RX’ stands for ‘Reception’. As the name suggests, transmission of data from the Arduino via serial communication takes place through the ‘TX’ pin and receiving information occurs through the ‘RX’ pin. There are LEDs connected to the board which also have ‘TX’ and ‘RX’ written next to them which serve as indicators of successful communication. The ‘TX’ LED will blink when data is transmitted and ‘RX’ LED will blink when data is being received. These pins can be used for digital I/O just like digital pins 2 – 13. However, they can’t be used when serial communication is happening. To recap, serial communication is the process of sending data one bit at a time, sequentially, over a communication channel. We need to set a parameter called ‘baud rate’ when using serial communication. Baud rate sets the data transfer rate in bits per second (baud) for serial data transmission. It indicates how quickly data is being transferred serially. To understand this better, consider the following analogy: If two people are having a conversation, the ‘baud rate’ is similar to how many words a person speaks to the other per minute. If one person is not a native speaker of the language, his/her ability to grasp the meaning words the other says will not be as effective as a native speaker. So, we speak slowly to help a non-native speaker of the language understand what we are saying better. The same thing happens with communicating devices as well. Sometimes, we need to ensure that the baud rate used for communication is compatible with the devices involved in the serial communication process. Both the devices involved in communication must operate at the same baud rate.
Common errors and helpful tips: You will not be able to upload your code to the Arduino if the pins D0 and D1 are being used to connect to another device. Ensure that devices attached to these pins are disconnected before uploading your code. In addition, if you are using an alternate form of communication (e.g. Bluetooth communication using ‘HC-05’ Bluetooth module), you will need to switch off your computer’s Bluetooth connection to upload your code to the Arduino, as two forms of serial communication cannot happen simultaneously. Again, taking the same analogy of people talking, this is like two people talking to a third person simultaneously. One person needs to talk at a time for successful communication of information to take place. Before selecting your baud rate, please refer to the data sheet any sensors that you are using in your circuits to ensure that they are compatible with the chosen baud rate.
- PWM pins
Recall that from the previous post, we referred to the analog pins as “Analog Input” pins, and not as Analog I/O pins. This is because analog outputs are given through some digital pins. Sounds strange right? Let’s see how this happens. If you look at your Arduino Uno board, you can see some digital I/O pins with the symbol ‘~’ marked next to it. This symbol indicates that it is a PWM pin. There are six such pins on the Arduino Uno, which are digital pins 3,5,6,9,10 & 11. PWM stands for “Pulse Width Modulation”, and these pins have the capability to produce analog outputs using a digital input signal. It is hence a type of DAC ( digital to analog converter).
So how does PWM work? The objective is that we need to convert a square wave which oscillates between HIGH (1) and LOW (0), to a continuously varying signal. Pulse Width Modulation allows us to vary how much time the signal is HIGH (1) to get a wave that closely resembles an analog signal. We can change the proportion of time the signal is HIGH compared to when it is LOW over a consistent time interval. By doing this, the width of the wave (or pulse) can be changed. Hence, this process is called Pulse Width Modulation (where modulation refers to the continuously changing width of the pulse). The duration of HIGH (1) time is called the pulse width.
To describe the amount of HIGH time, we use the concept of ‘duty cycle’. Duty cycle is expressed as a percentage. The percentage duty cycle specifically describes the percentage of time a digital signal is HIGH over an interval of time. This period is the inverse of the frequency of the waveform. For example, if the digital signal spends 25% of the time in the HIGH state and the other 75% in the LOW state, we would say the digital signal has a duty cycle of 25%. The graph below illustrates three scenarios.
- I2C pins
Sometimes we want to share the workload of one Arduino with another or maybe we want more analog or digital pins to interface it with more devices. In such a situation we can create a network of devices with one master device and multiple slave devices. This is called an Inter-Integrated Circuit or I2C. I2C is typically used to communicate between components on motherboards in any embedded system. I2C requires two digital lines: Serial Data line (SDA) to transfer data and Serial Clock Line (SCL) to keep the clock. Each I2C connection can have one master and multiple slaves. A master can write to slaves and request the slaves to give data. However, no slave can directly write to the master or to another slave. Every slave has a unique address on the bus, and the master needs to know the addresses of each slave it wants to access. A lot of sensors use I2C to communicate, typically Inertial Measurement Units (IMU) sensors, barometers, temperature sensors, and some sonars. On the Arduino Uno board, analog pin A4 is used as the SDA pin and analog pin A5 is used as the SCL pin.
- SPI pins
Serial Peripheral Interface is a type of synchronous (by synchronous, we mean that the clock data is also transferred) protocol used by microcontrollers for communicating with one or more peripheral devices quickly over short distances. SPI is also used for the same reasons as I2C i.e., to include multiple devices in the network. It can also be used for communication between two microcontrollers. With an SPI connection, there is always one master (microcontroller) which controls the peripheral devices. There are typically three lines through which communication takes place:
- MISO (Master In Slave Out) – The Slave line for sending data to the master,
- MOSI (Master Out Slave In) – The Master line for sending data to the peripherals,
- SCK (Serial Clock) – The clock pulses which synchronize data transmission generated by the master
There is also one line specific for every device:
- SS (Slave Select) – the pin on each device that the master can use to enable and disable specific devices. When a device’s Slave Select pin is low, it communicates with the master. When it’s high, it ignores the master.
On the Uno board, the pins are mapped as follows:
- MOSI – D11 or ICSP 4
- MISO – D12 or ICSP 1
- SCK – D13 or ICSP 3
- SS(slave) – D10
Which mode of communication is the best?
To summarize, there are three modes of communication that Arduino can support:
- Simple serial communication
- What’s wrong with simple serial communication?
For starters, serial communication can only support communication between two devices. More than two devices cannot be supported. Furthermore, the communication is asynchronous, which means clock data between the two devices is not transferred. As a result, both signals will be out of phase with each other. Hence, devices using them must agree ahead of time on a data rate. The two devices must also have clocks that are close to the same rate, and will remain so–excessive differences between clock rates on either end will result in garbage data. In addition, at least one start bit and one stop bit needs to be included for every 8 bits of data transferred (for an 8-bit processor). This means that 10 bits of transmission time is required for every 8-bits of data sent. These two additional bits will reduce the rate of data transfer. This may not be preferred for sensors which transmit continuously varying data.
- What are the drawbacks of SPI communication?
The major disadvantage of SPI communication is the number of pins required to establish communication with the Arduino. To connect a single master to a single slave, we require four lines. Also, each additional slave requires an additional I/O pin on the master. This is not preferred for applications that include a large number of peripheral devices. The large number of connections for each device can make routing signals more difficult in tight PCB layout situations. SPI only allows one master on the bus and multiple masters communicating with the same slave devices isn’t possible. SPI is ideal for high data rate full-duplex (simultaneous sending and receiving of data) connections, supporting clock rates more than 10MHz (10 million bits per second).
- I2C: The best of both worlds.
I2C provides a great balance between simple serial communication and SPI communication. It provides synchronous communication (like SPI), however, only using 2 pins for communication. However, the rate of data transfer isn’t as quick as SPI. I2C devices communicate at 100kHz or 400kHz. The hardware involved in I2C isn’t as complex as asynchronous simple serial communication, but more complex than SPI.
Hopefully, this post should give you a better insight into the internal communication mechanisms between peripherals and the microcontroller. In the next post, we will explore the world of sensors and learn how to integrate them into our applications using Arduino.