In this tutorial we explain how to add further serial interfaces to your SAMD based board.
AUTHOR: Arduino
LAST REVISION:
02/07/2023, 11:21 AM
In this tutorial we explain how to add further serial interfaces to your SAMD based board. These interfaces are hardware based and can be of I2C, UART, or SPI type. This is possible because the SAMD microcontroller has six internal serial modules that can be configured individually and just four of them are already configured. The other two are available for mapping onto specific pins. In this tutorial we explain how you can do that.
Hardware Required
- Arduino Zero, MKRZero or MKR1000 Board
Circuit
Only your Arduino Board is needed for this example.
Pin functions
One of the advantages of the Arduino platform is the simplification of the hardware, assigning to each microcontroller pin one of the many possible functions. You can find the various functions assigned to each pin in the variant.cpp file of each board. Let's see, for example, the variant.cpp for the MKR1000.
Focusing our attention on the SERCOM related pins, we can extract the following information:
1/*
2
3 +------------+------------------+---------+---------+----------+
4
5 | Pin number | MKR Board pin | Perip.C | Perip.D | Periph.G |
6
7 | | | SERCOMx | SERCOMx | COM |
8
9 | | | (x/PAD) | (x/PAD) | |
10
11 +------------+------------------+---------+---------+----------+
12
13 | 00 | D0 | 3/00 | 5/00 | |
14
15 | 01 | D1 | 3/01 | 5/01 | USB/SOF |
16
17 | 02 | D2 | 0/02 | 2/02 | I2S/SCK0 |
18
19 | 03 | D3 | 0/03 | 2/03 | I2S/FS0 |
20
21 | 04 | D4 | | 4/02 | I2S/MCK1 |
22
23 | 05 | D5 | | 4/03 | I2S/SCK1 |
24
25 | 06 | D6 | 5/02 | 3/02 | I2S/SCK0 |
26
27 | 07 | D7 | 5/03 | 3/03 | I2S/FS0 |
28
29 +------------+------------------+---------+---------+----------+
30
31 | | SPI | | | |
32
33 | 08 | MOSI | *1/00 | 3/00 | |
34
35 | 09 | SCK | *1/01 | 3/01 | |
36
37 | 10 | MISO | *1/03 | 3/03 | I2S/SD0 |
38
39 +------------+------------------+---------+---------+----------+
40
41 | | Wire | | | |
42
43 | 11 | SDA | *0/00 | 2/00 | I2S/SD1 |
44
45 | 12 | SCL | *0/01 | 2/01 | I2S/MCK0 |
46
47 +------------+------------------+---------+---------+----------+
48
49 | | Serial1 | | | |
50
51 | 13 | RX | | *5/03 | |
52
53 | 14 | TX | | *5/02 | |
(Video) #1 Say NO to ARDUINO! New ARM Microcontroller Programming and Circuit Building Series54
55 +------------+------------------+---------+---------+----------+
56
57 | 16 | A1 | | 5/00 | |
58
59 | 17 | A2 | | 5/01 | |
60
61 | 18 | A3 | | 0/00 | |
62
63 | 19 | A4 | | 0/01 | |
64
65 | 20 | A5 | | 0/02 | |
66
67 | 21 | A6 | | 0/03 | I2S/SD0 |
68
69 +------------+------------------+---------+---------+----------+
See AlsoUsing parseInt() with Arduino70
71 | | ATWINC1501B SPI | | | |
72
73 | 26 | WINC MOSI | *2/00 | 4/00 | |
74
75 | 27 | WINC SCK | *2/01 | 4/01 | |
76
77 | 28 | WINC SSN | 2/02 | 4/02 | |
78
79 | 29 | WINC MISO | *2/03 | 4/03 | |
80
81 +------------+------------------+---------+---------+----------+
82
83 | | ATWINC1501B PINS | | | |
84
85 | 32 | WINC WAKE | | 4/00 | |
86
87 | 33 | WINC IRQN | | 4/01 | |
88
89 +------------+------------------+---------+---------+----------+
90
91 */
As you can see, SERCOMs can be routed almost everywhere, having more than one SERCOM routable on more than one pin.
Default assigned SERCOMs
On the header of the MKR1000 boards, you can find an SPI, I2C and UART interface positioned as follows:
SPI / SERCOM 1:
MOSI on pin 8;
SCK on pin 9;
MISO on pin 10;
I2C / SERCOM 0:
SDA on pin 11;
SCL on pin 12;
UART / SERCOM 5:
RX on pin 13;
TX on pin 14;
Additionally there is another SPI interface internally connected to the WINC1500 module, wired as follows:
WINC1500 SPI / SERCOM 2:
MOSI on pin 26;
SCK on pin 27;
MISO on pin 29;
So removing from our table the pre-existing interfaces, since our aim is to add new interfaces instead of changing the pre-defined ones, we obtain what follows:
1/*
2
3 +------------+------------------+---------+---------+----------+
4
5 | Pin number | MKR Board pin | Perip.C | Perip.D | Periph.G |
6
7 | | | SERCOMx | SERCOMx | COM |
8
9 | | | (x/PAD) | (x/PAD) | |
10
(Video) Uploading Arduino bootloader on a new micro-controller // With J-Link SEGGER, SWD for MCU SAMD21G11 +------------+------------------+---------+---------+----------+
12
13 | 00 | D0 | 3/00 | 5/00 | |
14
15 | 01 | D1 | 3/01 | 5/01 | USB/SOF |
16
17 | 02 | D2 | 0/02 | 2/02 | I2S/SCK0 |
18
19 | 03 | D3 | 0/03 | 2/03 | I2S/FS0 |
20
21 | 04 | D4 | | 4/02 | I2S/MCK1 |
22
23 | 05 | D5 | | 4/03 | I2S/SCK1 |
24
25 | 06 | D6 | 5/02 | 3/02 | I2S/SCK0 |
26
27 | 07 | D7 | 5/03 | 3/03 | I2S/FS0 |
28
29 +------------+------------------+---------+---------+----------+
30
31 | 16 | A1 | | 5/00 | |
32
33 | 17 | A2 | | 5/01 | |
34
35 | 18 | A3 | | 0/00 | |
36
37 | 19 | A4 | | 0/01 | |
38
39 | 20 | A5 | | 0/02 | |
40
41 | 21 | A6 | | 0/03 | I2S/SD0 |
42
43 +------------+------------------+---------+---------+----------+
44
45 */
Adding a new communication interface
Let's now try to use the table above to add a new interface to our MKR1000 board.
Create a new Wire instance
As we can see, pin 0 and pin 1 can be driven by two SERCOMs. In particular by SERCOM3 and SERCOM5. Looking at the SAMD21 datasheet, we can figure out that the SERCOM PAD0 can be used as SDA and the SERCOM PAD1 as SCL. So we can do this using the example below.
1/* Wire Slave Sender on pins 0 and 1 on MKR1000
2
3 Demonstrates use of the Wire library and how to instantiate another Wire
4
5 Sends data as an I2C/TWI slave device
6
7 Refer to the "Wire Master Reader" example for use with this
8
9 Created 20 Jun 2016
10
11 by
12
13 Arturo Guadalupi <a.guadalupi@arduino.cc>
14
15 Sandeep Mistry <s.mistry@arduino.cc>
16
17*/
18
19#include <Wire.h>
20#include "wiring_private.h"
21
22TwoWire myWire(&sercom3, 0, 1); // Create the new wire instance assigning it to pin 0 and 1
23
24void setup()
25{
26
27 myWire.begin(2); // join i2c bus with address #2
28
29 pinPeripheral(0, PIO_SERCOM); //Assign SDA function to pin 0
(Video) LDmicro 11: Serial LCD Temperature Controller (Microcontroller PLC Ladder Programming with LDmicro)30
31 pinPeripheral(1, PIO_SERCOM); //Assign SCL function to pin 1
32
33 myWire.onRequest(requestEvent); // register event
34}
35
36void loop()
37{
38
39 delay(100);
40}
41
42// function that executes whenever data is requested by master
43// this function is registered as an event, see setup()
44void requestEvent()
45{
46
47 myWire.write("hello "); // respond with message of 6 bytes
48
49 // as expected by master
50}
51
52// Attach the interrupt handler to the SERCOM
53
54extern "C" {
55
56 void SERCOM3_Handler(void);
57
58 void SERCOM3_Handler(void) {
59
60 myWire.onService();
61
62 }
63}
the two instructions use the internal function
pinPeripheral (pinnumber, function)
that reassigns the pins
1pinPeripheral(0, PIO_SERCOM); //Assign SDA function to pin 0
2
3pinPeripheral(1, PIO_SERCOM); //Assign SCL function to pin 1
must be put in the
setup()
in order to override the standard Arduino pin assignment for this board (digital I/O) and to allow the SERCOM to drive them.
The callback
1void SERCOM3_Handler(void) {
2
3 myWire.onService();
4}
is used to allow the real I2C communication, since the Wire library relies on interrupts.
Create a new Serial instance
1/*
2
3 AnalogReadSerial on new UART placed on pins 1 and 0
4
5 Reads an analog input on pin A0, prints the result to the serial monitor.
6
7 Graphical representation is available using serial plotter (Tools > Serial Plotter menu)
8
9 Attach the center pin of a potentiometer to pin A0, and the outside pins to +3.3V and ground.
10
11 Short together pin 1 and pin 0 with a wire jumper
12
13 Created 20 Jun 2016
14
15 by
16
17 Arturo Guadalupi <a.guadalupi@arduino.cc>
(Video) SPI protocol features and how to integrate multiple sensors // Arduino Zero, LoRa and E-Ink modules18
19 This example code is in the public domain.
20
21*/
22
23#include <Arduino.h>
24#include "wiring_private.h"
25
26Uart mySerial (&sercom3, 1, 0, SERCOM_RX_PAD_1, UART_TX_PAD_0); // Create the new UART instance assigning it to pin 1 and 0
27
28// the setup routine runs once when you press reset:
29void setup() {
30
31 // initialize serial communication at 9600 bits per second:
32
33 Serial.begin(9600);
34
35 mySerial.begin(9600);
36
37 pinPeripheral(1, PIO_SERCOM); //Assign RX function to pin 1
38
39 pinPeripheral(0, PIO_SERCOM); //Assign TX function to pin 0
40}
41
42// the loop routine runs over and over again forever:
43void loop() {
44
45 // read the input on analog pin 0:
46
47 int sensorValue = analogRead(A0);
48
49 // print out the value you read on mySerial wired in loopback:
50
51 mySerial.write(sensorValue);
52
53 while (mySerial.available()) {
54
55 Serial.print(mySerial.read());
56
57 }
58
59 Serial.println();
60
61 delay(1); // delay in between reads for stability
62}
63
64// Attach the interrupt handler to the SERCOM
65void SERCOM3_Handler()
66{
67
68 mySerial.IrqHandler();
69}
as we did for Wire, the two instructions
1pinPeripheral(1, PIO_SERCOM); //Assign RX function to pin 1
2
3pinPeripheral(0, PIO_SERCOM); //Assign TX function to pin 0
must be placed in order to override the standard Arduino pin assignment for this board (digital I/O) and to allow the SERCOM to drive them.
The callback
1void SERCOM3_Handler()
2{
3
4 mySerial.IrqHandler();
5}
is used to allow the real Serial communication, since the Serial library relies on interrupts too.
FAQs
How many software serials does Arduino have? ›
The SoftwareSerial library allows serial communication on other digital pins of an Arduino board, using software to replicate the functionality (hence the name "SoftwareSerial"). It is possible to have multiple software serial ports with speeds up to 115200 bps.
Can Arduino have 2 serials? ›Use two of the serial ports available on the Arduino Mega. Sometimes, one serial port just isn't enough! When trying to communicate with multiple serial enabled devices, while also sending info back to the main serial window, a few extra RX/TX ports can be a welcomed thing.
What does statement serial begin 9600 mean? ›Serial. begin(9600); passes the value 9600 to the speed parameter. This tells the Arduino to get ready to exchange messages with the Serial Monitor at a data rate of 9600 bits per second. That's 9600 binary ones or zeros per second, and is commonly called a baud rate.
How many ports are in serial? ›There are two kinds of serial ports: DB25 and DB9, where DB25 is a 25-pin connection, and DB9 is a 9-pin connection. A serial port is a male port that can only send one bit of data at a time, whereas a parallel port is a female port that can send several bits at the same time.
Can we use multiple software serial? ›It is possible to have multiple software serial ports with speeds up to 115200 bps. A parameter enables inverted signaling for devices which require that protocol. The version of SoftwareSerial included in 1.0 and later is based on the NewSoftSerial library developed by Mikal Hart.