Skip to content

Commit 0d8f12d

Browse files
committed
Content update (SPI section)
1 parent 0b2ba68 commit 0d8f12d

File tree

1 file changed

+194
-17
lines changed
  • content/hardware/03.nano/boards/nano-r4/tutorials/01.user-manual

1 file changed

+194
-17
lines changed

content/hardware/03.nano/boards/nano-r4/tutorials/01.user-manual/content.md

Lines changed: 194 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -186,8 +186,9 @@ user LED of the Arduino Nano R4 board.
186186
#define LED_PIN LED_BUILTIN
187187
188188
void setup() {
189-
// Initialize serial communication at 115200 baud
189+
// Initialize serial communication and wait up to 2.5 seconds for a connection
190190
Serial.begin(115200);
191+
for (auto startNow = millis() + 2500; !Serial && millis() < startNow; delay(500));
191192
192193
// Configure LED pin as output
193194
pinMode(LED_PIN, OUTPUT);
@@ -265,8 +266,9 @@ RGB LED of the Arduino Nano R4 board.
265266
*/
266267
267268
void setup() {
268-
// Initialize serial communication at 115200 baud
269+
// Initialize serial communication and wait up to 2.5 seconds for a connection
269270
Serial.begin(115200);
271+
for (auto startNow = millis() + 2500; !Serial && millis() < startNow; delay(500));
270272
271273
// Initialize LEDR, LEDG and LEDB as outputs
272274
pinMode(LEDR, OUTPUT);
@@ -349,8 +351,9 @@ user LED of the Arduino Nano R4 board.
349351
*/
350352
351353
void setup() {
352-
// Initialize serial communication at 115200 baud
354+
// Initialize serial communication and wait up to 2.5 seconds for a connection
353355
Serial.begin(115200);
356+
for (auto startNow = millis() + 2500; !Serial && millis() < startNow; delay(500));
354357
355358
// Configure LED_BUILTIN pin as output
356359
pinMode(LED_BUILTIN, OUTPUT);
@@ -502,8 +505,9 @@ int lastButtonState = HIGH;
502505
bool ledState = false;
503506
504507
void setup() {
505-
// Initialize serial communication at 115200 baud
508+
// Initialize serial communication and wait up to 2.5 seconds for a connection
506509
Serial.begin(115200);
510+
for (auto startNow = millis() + 2500; !Serial && millis() < startNow; delay(500));
507511
508512
// Configure pins
509513
pinMode(buttonPin, INPUT_PULLUP);
@@ -617,8 +621,9 @@ and display the value on the Serial Monitor.
617621
const int analogPin = A0;
618622
619623
void setup() {
620-
// Initialize serial communication at 115200 baud
624+
// Initialize serial communication and wait up to 2.5 seconds for a connection
621625
Serial.begin(115200);
626+
for (auto startNow = millis() + 2500; !Serial && millis() < startNow; delay(500));
622627
623628
Serial.println("- Arduino Nano R4 - Analog Input Example started...");
624629
Serial.println("- Reading analog values from pin A0");
@@ -672,8 +677,9 @@ for precise analog input measurements.
672677
const int analogPin = A0;
673678
674679
void setup() {
675-
// Initialize serial communication at 115200 baud
680+
// Initialize serial communication and wait up to 2.5 seconds for a connection
676681
Serial.begin(115200);
682+
for (auto startNow = millis() + 2500; !Serial && millis() < startNow; delay(500));
677683
678684
// Set analog read resolution to 14-bit (0 - 16383)
679685
analogReadResolution(14);
@@ -758,8 +764,9 @@ the brightness of the built-in user LED of the Nano R4 board.
758764
const int ledPin = LED_BUILTIN;
759765
760766
void setup() {
761-
// Initialize serial communication at 115200 baud
767+
// Initialize serial communication and wait up to 2.5 seconds for a connection
762768
Serial.begin(115200);
769+
for (auto startNow = millis() + 2500; !Serial && millis() < startNow; delay(500));
763770
764771
// No need to set pinMode for PWM pins - analogWrite() handles this
765772
@@ -813,8 +820,9 @@ for precise control of the built-in orange user LED brightness.
813820
const int pwmPin = LED_BUILTIN;
814821
815822
void setup() {
816-
// Initialize serial communication at 115200 baud
823+
// Initialize serial communication and wait up to 2.5 seconds for a connection
817824
Serial.begin(115200);
825+
for (auto startNow = millis() + 2500; !Serial && millis() < startNow; delay(500));
818826
819827
// Set PWM resolution to 12-bit (0-4095)
820828
analogWriteResolution(12);
@@ -880,7 +888,7 @@ The Nano R4's OPAMP offers the following electrical characteristics:
880888
| Load Current | -100 to +100 | -100 to +100 | μA | Maximum |
881889
| Load Capacitance | 20 | 20 | pF | Maximum |
882890

883-
You can configure and use the OPAMP using the dedicated `<OPAMP.h>` library, which is included in the Arduino UNO R4 Boards core. To startup the OPAMP, simply include the library and call `OPAMP.begin(speed)` where speed can be `OPAMP_SPEED_LOWSPEED` for lower power consumption or `OPAMP_SPEED_HIGHSPEED` for better performance.
891+
You can configure and use the OPAMP using the dedicated `OPAMP.h` library, which is included in the Arduino UNO R4 Boards core. To startup the OPAMP, simply include the library and call `OPAMP.begin(speed)` where speed can be `OPAMP_SPEED_LOWSPEED` for lower power consumption or `OPAMP_SPEED_HIGHSPEED` for better performance.
884892

885893
The following example demonstrates how to initialize and use the OPAMP. The same code works for both voltage follower and amplifier configurations, **the difference is only in the external connections**:
886894

@@ -898,8 +906,9 @@ Works for both voltage follower and amplifier configurations.
898906
#include <OPAMP.h>
899907
900908
void setup() {
901-
// Initialize serial communication at 115200 baud
909+
// Initialize serial communication and wait up to 2.5 seconds for a connection
902910
Serial.begin(115200);
911+
for (auto startNow = millis() + 2500; !Serial && millis() < startNow; delay(500));
903912
904913
Serial.println("- Arduino Nano R4 - OPAMP Example started...");
905914
Serial.println("- Initializing OPAMP in high-speed mode...");
@@ -1000,8 +1009,9 @@ precise analog voltages on pin A0.
10001009
*/
10011010
10021011
void setup() {
1003-
// Initialize serial communication at 115200 baud
1012+
// Initialize serial communication and wait up to 2.5 seconds for a connection
10041013
Serial.begin(115200);
1014+
for (auto startNow = millis() + 2500; !Serial && millis() < startNow; delay(500));
10051015
10061016
// Set DAC resolution to 12-bit for maximum precision
10071017
analogWriteResolution(12);
@@ -1055,8 +1065,9 @@ for testing and signal generation applications.
10551065
*/
10561066
10571067
void setup() {
1058-
// Initialize serial communication at 115200 baud
1068+
// Initialize serial communication and wait up to 2.5 seconds for a connection
10591069
Serial.begin(115200);
1070+
for (auto startNow = millis() + 2500; !Serial && millis() < startNow; delay(500));
10601071
10611072
// Set DAC resolution to 12-bit for smooth waveform
10621073
analogWriteResolution(12);
@@ -1115,7 +1126,7 @@ The Nano R4's RTC offers the following technical specifications:
11151126
| Current Consumption | ~1 μA | In backup mode |
11161127
| Temperature Range | -40°C to +85°C | Operating range |
11171128

1118-
You can configure and use the RTC using the dedicated `<RTC.h>` library, which is included in the Arduino UNO R4 Boards core. The library provides functions to set time, get time, check RTC status and configure alarms.
1129+
You can configure and use the RTC using the dedicated `RTC.h` library, which is included in the Arduino UNO R4 Boards core. The library provides functions to set time, get time, check RTC status and configure alarms.
11191130

11201131
The following example demonstrates how to initialize the RTC and display the current date and time:
11211132

@@ -1133,8 +1144,9 @@ to keep track of date and time.
11331144
#include "RTC.h"
11341145
11351146
void setup() {
1136-
// Initialize serial communication at 115200 baud
1147+
// Initialize serial communication and wait up to 2.5 seconds for a connection
11371148
Serial.begin(115200);
1149+
for (auto startNow = millis() + 2500; !Serial && millis() < startNow; delay(500));
11381150
11391151
Serial.println("- Arduino Nano R4 - RTC Basic Example started...");
11401152
Serial.println("- Initializing RTC...");
@@ -1258,7 +1270,7 @@ The Nano R4's EEPROM offers the following technical specifications:
12581270
| Retention Time | 10+ years | Data retention period |
12591271
| Operating Voltage | +5 VDC | Same as board |
12601272

1261-
You can read and write to EEPROM using the dedicated `<EEPROM.h>` library, which is included in the Arduino UNO R4 Boards core. The library provides simple functions to store and retrieve individual bytes or larger data structures.
1273+
You can read and write to EEPROM using the dedicated `EEPROM.h` library, which is included in the Arduino UNO R4 Boards core. The library provides simple functions to store and retrieve individual bytes or larger data structures.
12621274

12631275
The following example demonstrates how to store and retrieve data from EEPROM:
12641276

@@ -1276,8 +1288,9 @@ from the built-in EEPROM memory.
12761288
#include <EEPROM.h>
12771289
12781290
void setup() {
1279-
// Initialize serial communication at 115200 baud
1291+
// Initialize serial communication and wait up to 2.5 seconds for a connection
12801292
Serial.begin(115200);
1293+
for (auto startNow = millis() + 2500; !Serial && millis() < startNow; delay(500));
12811294
12821295
Serial.println("- Arduino Nano R4 - EEPROM Basic Example started...");
12831296
@@ -1328,7 +1341,7 @@ You can open the Arduino IDE's Serial Monitor (Tools > Serial Monitor) to see th
13281341

13291342
![Arduino IDE Serial Monitor output for the EEPROM example sketch](assets/eeprom-1.png)
13301343

1331-
For storing larger data structures, you can use the `EEPROM.get()` and `EEPROM.put()` functions:
1344+
You can also store complex data structures using the `EEPROM.put()` and `EEPROM.get()` functions:
13321345

13331346
```arduino
13341347
// Define a structure for complex data
@@ -1354,6 +1367,170 @@ When working with EEPROM on the Nano R4, there are several key points to keep in
13541367
- Keep in mind that EEPROM stores data as individual bytes (0 to 255), so larger data types need to be broken down or use the `put()` and `get()` functions for automatic handling.
13551368
- The EEPROM retains data for over 10 years under normal conditions, making it excellent for long-term storage of configuration data and user preferences.
13561369

1370+
## SPI Communication
1371+
1372+
The Nano R4 board features built-in SPI (Serial Peripheral Interface) communication that allows your projects to communicate with external devices like sensors, displays, memory cards and other microcontrollers. SPI is implemented within the RA4M1 microcontroller and uses four dedicated pins to provide high-speed synchronous serial communication.
1373+
1374+
SPI is particularly useful when your project needs to communicate with external components at high speeds, rather than using slower protocols. While I²C is perfect for simple sensor communication and UART for basic serial data exchange, SPI excels at high-speed communication with devices like SD cards, TFT displays, wireless modules or external memory chips. SPI can achieve much faster data rates than I²C and can handle multiple devices on the same bus through individual chip select lines.
1375+
1376+
The Nano R4's SPI interface offers the following technical specifications:
1377+
1378+
| **Parameter** | **Value** | **Notes** |
1379+
|:-----------------:|:------------:|:---------------------------:|
1380+
| Clock Speed | Up to 24 MHz | Maximum SPI frequency |
1381+
| Data Transfer | 8-bit | Standard data width |
1382+
| Communication | Full-duplex | Simultaneous send/receive |
1383+
| SPI Pins | `D10`-`D13` | `CS`, `MOSI`, `MISO`, `SCK` |
1384+
| Multiple Devices | Supported | Via different `CS` pins |
1385+
| Operating Voltage | +5 VDC | Same as board |
1386+
| Protocol Support | Mode 0,1,2,3 | All SPI modes available |
1387+
1388+
The Nano R4 board uses the following pins for SPI communication:
1389+
1390+
| **Arduino Pin** | **Microcontroller Pin** | **SPI Function** | **Description** |
1391+
|:---------------:|:-----------------------:|:----------------:|:--------------------:|
1392+
| `D10` | `P103` | `CS` | Chip Select |
1393+
| `D11` | `P101` | `MOSI` | Master Out, Slave In |
1394+
| `D12` | `P100` | `MISO` | Master In, Slave Out |
1395+
| `D13` | `P102` | `SCK` | Serial Clock |
1396+
1397+
You can communicate via SPI using the dedicated `SPI.h` library, which is included in the Arduino UNO R4 Boards core. The library provides simple functions to initialize the bus, send and receive data and manage multiple devices.
1398+
1399+
The following example demonstrates how to use SPI communication to control an external device:
1400+
1401+
```arduino
1402+
/**
1403+
SPI Basic Example for the Arduino Nano R4 Board
1404+
Name: nano_r4_spi_basic.ino
1405+
Purpose: This sketch demonstrates how to use SPI communication
1406+
to send and receive data.
1407+
1408+
@author Arduino Product Experience Team
1409+
@version 1.0 01/06/25
1410+
*/
1411+
1412+
#include <SPI.h>
1413+
1414+
// Chip Select pin for SPI device
1415+
const int CS_PIN = 10;
1416+
1417+
void setup() {
1418+
// Initialize serial communication and wait up to 2.5 seconds for a connection
1419+
Serial.begin(115200);
1420+
for (auto startNow = millis() + 2500; !Serial && millis() < startNow; delay(500));
1421+
1422+
Serial.println("- Arduino Nano R4 - SPI Basic Example started...");
1423+
1424+
// Set CS pin as output and set it HIGH (inactive)
1425+
pinMode(CS_PIN, OUTPUT);
1426+
digitalWrite(CS_PIN, HIGH);
1427+
1428+
// Initialize SPI communication
1429+
SPI.begin();
1430+
1431+
// Configure SPI settings
1432+
// - Clock speed: 1 MHz (1000000 Hz)
1433+
// - Data order: Most Significant Bit first
1434+
// - Data mode: Mode 0 (Clock polarity = 0, Clock phase = 0)
1435+
SPI.beginTransaction(SPISettings(1000000, MSBFIRST, SPI_MODE0));
1436+
1437+
Serial.println("- SPI initialized successfully");
1438+
Serial.println("- Ready to communicate with SPI devices");
1439+
1440+
// Example: Send some test data
1441+
sendSPIData();
1442+
}
1443+
1444+
void loop() {
1445+
// Send a counter value every 2 seconds
1446+
static int counter = 0;
1447+
1448+
// Select the device (CS LOW)
1449+
digitalWrite(CS_PIN, LOW);
1450+
1451+
// Send counter value
1452+
byte response = SPI.transfer(counter);
1453+
1454+
// Deselect the device (CS HIGH)
1455+
digitalWrite(CS_PIN, HIGH);
1456+
1457+
// Display results
1458+
Serial.print("- Sent: ");
1459+
Serial.print(counter);
1460+
Serial.print(" | Received: ");
1461+
Serial.println(response);
1462+
1463+
// Increment counter and wrap around at 255
1464+
counter++;
1465+
if (counter > 255) {
1466+
counter = 0;
1467+
}
1468+
1469+
delay(2000);
1470+
}
1471+
1472+
void sendSPIData() {
1473+
Serial.println("- Sending test data...");
1474+
1475+
// Select the device
1476+
digitalWrite(CS_PIN, LOW);
1477+
1478+
// Send a sequence of test bytes
1479+
for (int i = 0; i < 5; i++) {
1480+
byte testData = 0x10 + i; // Send 0x10, 0x11, 0x12, 0x13, 0x14
1481+
byte response = SPI.transfer(testData);
1482+
1483+
Serial.print(" Sent: 0x");
1484+
if (testData < 16) Serial.print("0");
1485+
Serial.print(testData, HEX);
1486+
Serial.print(" | Received: 0x");
1487+
if (response < 16) Serial.print("0");
1488+
Serial.println(response, HEX);
1489+
1490+
delay(100);
1491+
}
1492+
1493+
// Deselect the device
1494+
digitalWrite(CS_PIN, HIGH);
1495+
1496+
Serial.println("- Test data transmission complete");
1497+
}
1498+
```
1499+
1500+
***To test this example, no external SPI device is required. The code will demonstrate SPI communication patterns, though without a connected device, the received data will typically be `0xFF`.***
1501+
1502+
You can open the Arduino IDE's Serial Monitor (Tools > Serial Monitor) to see the SPI communication in action. The example shows how to properly select devices, send data and handle responses.
1503+
1504+
For connecting multiple SPI devices, you can use different digital pins as additional Chip Select (`CS`) lines while sharing the `MOSI`, `MISO` and `SCK` pins:
1505+
1506+
```arduino
1507+
// Multiple device example
1508+
const int DEVICE1_CS = 10; // First SPI device
1509+
const int DEVICE2_CS = 9; // Second SPI device
1510+
const int DEVICE3_CS = 8; // Third SPI device
1511+
1512+
void setup() {
1513+
SPI.begin();
1514+
1515+
// Configure all CS pins
1516+
pinMode(DEVICE1_CS, OUTPUT);
1517+
pinMode(DEVICE2_CS, OUTPUT);
1518+
pinMode(DEVICE3_CS, OUTPUT);
1519+
1520+
// Set all CS pins HIGH (inactive)
1521+
digitalWrite(DEVICE1_CS, HIGH);
1522+
digitalWrite(DEVICE2_CS, HIGH);
1523+
digitalWrite(DEVICE3_CS, HIGH);
1524+
}
1525+
```
1526+
1527+
When working with SPI on the Nano R4, there are several key points to keep in mind for successful implementation:
1528+
1529+
- The SPI protocol requires careful attention to timing and device selection. Always ensure that only one device is selected (`CS LOW`) at a time, and remember to deselect devices (`CS HIGH`) after communication to avoid conflicts.
1530+
- Different SPI devices may require different clock speeds and modes, so check your device's datasheet for the correct `SPISettings()` parameters.
1531+
- Keep in mind that SPI is a synchronous protocol, meaning that data is transferred in both directions simultaneously with each clock pulse. Even if you only need to send data, you'll still receive data back, and vice versa.
1532+
- The Nano R4 board can communicate with multiple SPI devices by using different Chip Select (`CS`) pins, making it perfect for complex projects that need to interface with various sensors, displays and storage devices.
1533+
13571534
## Support
13581535

13591536
If you encounter any issues or have questions while working with your Nano R4 board, we provide various support resources to help you find answers and solutions.

0 commit comments

Comments
 (0)