8000 [MKC-1048] Add UNO R4 WiFi CodeBlocks by ArduinoBot · Pull Request #1245 · arduino/docs-content · GitHub
[go: up one dir, main page]

Skip to content

[MKC-1048] Add UNO R4 WiFi CodeBlocks #1245

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Aug 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ tags:
- ADC
- 14-bit
author: 'Karl Söderby'
hardware:
- hardware/02.hero/boards/uno-r4-wifi
---

In this tutorial you will learn how to change the analog-to-digital converter (ADC) on an **Arduino UNO R4 WiFi** board. By default, the resolution is set to 10-bit, which can be updated to both 12-bit (0-4096) and 14-bit (0-16383) resolutions for improved accuracy on analog readings.
Expand Down
75 changes: 5 additions & 70 deletions content/hardware/02.hero/boards/uno-r4-wifi/tutorials/can/can.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ description: 'Learn how to send messages using the CAN bus on the UNO R4 WiFi.'
tags:
- CAN
author: 'Karl Söderby'
hardware:
- hardware/02.hero/boards/uno-r4-wifi
---

In this tutorial you will learn how to use the CAN controller on the **Arduino UNO R4 WiFi** board. The CAN controller is embedded in the UNO R4 WiFi's microcontroller (RA4M1). CAN is a serial protocol that is mainly used in the automotive industry.
Expand All @@ -25,7 +27,7 @@ The goals of this tutorial are:
- CAN transceiver module *
- Jumper wires

* In this tutorial, we are using a SN65HVD230 breakout module.
\* In this tutorial, we are using a SN65HVD230 breakout module.

## Controller Area Network (CAN)

Expand Down Expand Up @@ -79,80 +81,13 @@ CanMsg msg(CAN_ID, sizeof(msg_data), msg_data);

After you have crafted a CAN message, we can send it off, by using the `CAN.write()` method. The following example creates a CAN message that increases each time `void loop()` is executed.

```arduino
#include <Arduino_CAN.h>

static uint32_t const CAN_ID = 0x20;

void setup()
{
Serial.begin(115200);
while (!Serial) { }

if (!CAN.begin(CanBitRate::BR_250k))
{
Serial.println("CAN.begin(...) failed.");
for (;;) {}
}
}

static uint32_t msg_cnt = 0;

void loop()
{
/* Assemble a CAN message with the format of
* 0xCA 0xFE 0x00 0x00 [4 byte message counter]
*/
uint8_t const msg_data[] = {0xCA,0xFE,0,0,0,0,0,0};
memcpy((void *)(msg_data + 4), &msg_cnt, sizeof(msg_cnt));
CanMsg msg(CAN_ID, sizeof(msg_data), msg_data);

/* Transmit the CAN message, capture and display an
* error core in case of failure.
*/
if (int const rc = CAN.write(msg); rc < 0)
{
Serial.print ("CAN.write(...) failed with error c 10000 ode ");
Serial.println(rc);
for (;;) { }
}

/* Increase the message counter. */
msg_cnt++;

/* Only send one message per second. */
delay(1000);
}
```
<CodeBlock url="https://github.com/arduino/ArduinoCore-renesas/blob/main/libraries/Arduino_CAN/examples/CANWrite/CANWrite.ino" className="arduino"/>

### CAN Read

To read an incoming CAN message, first use `CAN.available()` to check if data is available, before using `CAN.read()` to read the message.

```arduino
#include <Arduino_CAN.h>

void setup()
{
Serial.begin(115200);
while (!Serial) { }

if (!CAN.begin(CanBitRate::BR_250k))
{
Serial.println("CAN.begin(...) failed.");
for (;;) {}
}
}

void loop()
{
if (CAN.available())
{
CanMsg const msg = CAN.read();
Serial.println(msg);
}
}
```
<CodeBlock url="https://github.com/arduino/ArduinoCore-renesas/blob/main/libraries/Arduino_CAN/examples/CANRead/CANRead.ino" className="arduino"/>

## Summary

Expand Down
166 changes: 5 additions & 161 deletions content/hardware/02.hero/boards/uno-r4-wifi/tutorials/dac/dac.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,26 +54,7 @@ The waveform is being stored as samples in an array, and with every loop of the

Open a new sketch and paste the following code into your window.

```arduino
#include "analogWave.h"

analogWave wave(DAC);

int freq = 10; // in hertz, change accordingly

void setup() {
Serial.begin(115200);
pinMode(A5, INPUT);
wave.sine(freq);
}

void loop() {
freq = map(analogRead(A5), 0, 1024, 0, 10000);
Serial.println("Frequency is now " + String(freq) + " hz");
wave.freq(freq);
delay(1000);
}
```
<CodeBlock url="https://github.com/arduino/ArduinoCore-renesas/blob/main/libraries/AnalogWave/examples/SineWave/SineWave.ino" className="arduino"/>

## Testing It Out
Once you have uploaded the code to the board, it should start generating a sine wave oscillation on the DAC, that depending on the frequency could be used to produce sound on a piezo buzzer or speaker. If you have an oscilloscope at hand, connecting its probe to the DAC output might be an interesting exercise so see what the wave looks like.
Expand All @@ -87,149 +68,12 @@ Now that you know your setup is working, you can experiment further with differe
### Frere Jacques

This one for example plays the melody of Frere Jacques:
```arduino
/*
DAC Melody player

Generates a series of tones from MIDI note values
using the Uno R4 DAC and the AnalogWave Library.
The melody is "Frere Jacques"

circuit:
* audio amp (LM386 used for testing) input+ attached to A0
* audio amp input- attached to ground
* 4-8-ohm speaker attached to amp output+
* Potentiometer connected to pin A5

created 13 Feb 2017
modified 3 Jul 2023
by Tom Igoe
*/
#include "analogWave.h"
analogWave wave(DAC);

#define NOTE_A4 69 // MIDI note value for middle A
#define FREQ_A4 440 // frequency for middle A

// the tonic, or first note of the key signature for the song:
int tonic = 65;
// the melody sequence. Note values are relative to the tonic:
int melody[] = {1, 3, 5, 1,
1, 3, 5, 1,
5, 6, 8, 5, 6, 8,
8, 10, 8, 6, 5, 1,
8, 10, 8, 6, 5, 1,
1, -4, 1,
1, -4, 1
};
// the rhythm sequence. Values are 1/note, e.g. 4 = 1/4 note:
int rhythm[] = {4, 4, 4, 4,
4, 4, 4, 4,
4, 4, 2,
4, 4, 2,
8, 8, 8, 8, 4, 4,
8, 8, 8, 8, 4, 4,
4, 4, 2,
4, 4, 2
};
// which note of the melody to play:
int noteCounter = 0;

int bpm = 120; // beats per minute
// duration of a beat in ms
float beatDuration = 60.0 / bpm * 1000;

void setup() {
// start the sine wave generator:
wave.sine(10);
}

void loop() {
// current note is an element of the array:
int currentNote = melody[noteCounter] + tonic;
// play a note from the melody:
// convert MIDI note number to frequency:
float frequency = FREQ_A4 * pow(2, ((currentNote - NOTE_A4) / 12.0));

// all the notes in this are sixteenth notes,
// which is 1/4 of a beat, so:
float noteDuration = beatDuration * (4.0 / rhythm[noteCounter]);
// turn the note on:
wave.freq(frequency);
// tone(speakerPin, frequency, noteDuration * 0.85);
// keep it on for the appropriate duration:
delay(noteDuration * 0.85);
wave.stop();
delay(noteDuration * 0.15);
// turn the note off:
// noTone(speakerPin);
// increment the note number for next time through the loop:
noteCounter++;
// keep the note in the range from 0 - 32 using modulo:
noteCounter = noteCounter % 32;

}
```
<CodeBlock url="https://github.com/arduino/ArduinoCore-renesas/blob/main/libraries/AnalogWave/examples/DACJacques/DACJacques.ino" className="arduino"/>

### MIDI Piano Notes
This sketch will break down the potentiometer input to steps, that are translated to the 88 MIDI notes that represent the keys on a piano.

```arduino
/*
Plays a tone in response to a potentiometer
formula from https://newt.phys.unsw.edu.au/jw/notes.html
and https://en.wikipedia.org/wiki/MIDI_tuning_standard:

the MIDI protocol divides the notes of an equal-tempered scale into
128 possible note values. Middle A is MIDI note value 69. There is
a formula for converting MIDI note numbers (0-127) to pitches. This sketch
reduces that to the notes 21 - 108, which are the 88 keys found on a piano:

frequency = 440 * ((noteNumber - 69) / 12.0)^2

You can see this applied in the code below.

circuit:
* audio amp (LM386 used for testing) input+ attached to A0
* audio amp input- attached to ground
* 4-8-ohm speaker attached to amp output+
* Potentiometer connected to pin A5

created 18 Dec 2018
modified 3 Jul 2023
by Tom Igoe
*/

// include the AnalogWave library:
#include "analogWave.h"
analogWave wave(DAC);

// middle A is the reference frequency for an
// equal-tempered scale. Set its frequency and note value:
#define NOTE_A4 69 // MIDI note value for middle A
#define FREQ_A4 440 // frequency for middle A

const int speakerPin = A0; // the pin number for the speaker
void setup() {
Serial.begin(9600);
wave.sine(10);
}
void loop() {
// convert sensor reading to 21 - 108 range
// which is the range of MIDI notes on an 88-key keyboard
// (from A0 to C8):
int sensorReading = analogRead(A5);
int noteValue = map(sensorReading, 0, 1023, 21, 108);
// then convert to frequency:
float frequency = FREQ_A4 * pow(2, ((noteValue - NOTE_A4) / 12.0));
int freq = int(frequency);
// turn the speaker on:
wave.freq(freq);
Serial.print("note value: "+ String(noteValue) + " freq: ");
Serial.println(freq);
delay(500);
}
```
This sketch will break down the potentiometer input into steps, that are translated to the 88 MIDI notes that represent the keys on a piano.

<CodeBlock url="https://github.com/arduino/ArduinoCore-renesas/blob/main/libraries/AnalogWave/examples/DACEqualTemperedScale/DACEqualTemperedScale.ino" className="arduino"/>

## Conclusion
By following this tutorials you've experimented with the DAC on the Arduino UNO R4 boards and used it to first generate a sine wave, and then to explore the possibilities of analog output by testing out various examples.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ tags:
- RTC
- Alarm
author: 'Karl Söderby'
hardware:
- hardware/02.hero/boards/uno-r4-wifi
---

In this tutorial you will learn how to access the EEPROM (memory) on an **Arduino UNO R4 WiFi** board. The EEPROM is embedded in the UNO R4 WiFi's microcontroller (RA4M1).
Expand Down Expand Up @@ -42,7 +44,7 @@ EEPROM.read(0); //reads first byte

There are several more methods available when working with EEPROM, and you can read more about this in the [A Guide to EEPROM](https://docs.arduino.cc/learn/programming/eeprom-guide).

***Please note: EEPROM is a type of memory with limited amount of write cycles. Be cautious when writing to this memory as you may significantly reduce the lifespan of this memory.***
***Please note: EEPROM is a type of memory with a limited amount of write cycles. Be cautious when writing to this memory as you may significantly reduce the lifespan of this memory.***

### EEPROM Write

Expand All @@ -55,31 +57,31 @@ int addr = 0;
byte value = 100;

void setup() {
EEPROM.write(addr, val);
EEPROM.write(addr, value);
}
void loop(){
}
```

### EEPROM Read

A minimal example on how to **read** from the EEPROM can be found below:
A minimal example of how to **read** from the EEPROM can be found below:

```arduino
#include <EEPROM.h>

int address = 0;
int addr = 0;
byte value;

void setup() {
Serial.begin(9600);
value = EEPROM.read(addr);
while (!Serial) {
;

}

Serial.print("Address 0: ");
Serial.println(addr);
Serial.println(value);
}

void loop() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
title: Getting Started wi C0BA th UNO R4 WiFi
description: A step-by-step guide to install the board package needed for the UNO R4 WiFi board.
author: Hannes Siebeneicher
hardware:
- hardware/02.hero/boards/uno-r4-wifi
tags: [UNO R4 WiFi, Installation, IDE]
---

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ tags:
- RTC
- Alarm
author: 'Karl Söderby'
hardware:
- hardware/02.hero/boards/uno-r4-wifi
---

In this tutorial you will learn how to access the real-time clock (RTC) on an **Arduino UNO R4 WiFi** board. The RTC is embedded in the UNO R4 WiFi's microcontroller (RA4M1).
Expand Down
Loading
0