251 views|1 replies

25

Posts

1

Resources
The OP
 

【Follow me Season 2 Episode 2】+LED matrix, DAC, amplifier, AD [Copy link]

 
In the previous article, we briefly learned how to operate the GPIO of Arduino R4 WIFI and how to print data through the serial port. This article will record the learning of the special peripheral LED matrix on this board, as well as the analog-related DAC, ADC and amplifier-related content.
1. Onboard LED Matrix
First, you should include the matrix library file provided by Arduino #include "Arduino_LED_Matrix.h"
Then in this file there is an ArduinoLEDMatrix class, we use this class to define a new object matrix. The code is implemented as ArduinoLEDMatrix matrix;
Initialize the matrix matrix.begin();
Define the matrix display. The display of the matrix is nothing more than controlling each LED to be at a high level or a low level. When the corresponding LED is at a high level, the LED lights up, and at the same time, the corresponding position number in the matrix is 1. On the contrary, it is at a low level, the LED goes out, and the corresponding position number in the matrix is 0.
The LED matrix on the Arduino R4 WIFI board is 8*12, so we define a two-dimensional array to store the data for displaying graphics. Here, three graphics are set, namely, a smiling face, a blinking face, and a crying face.
uint8_t smile[8][12] = {
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 1, 1, 0, 0, 0, 0, 1, 1, 0, 0 },
{ 0, 0, 1, 1, 0, 0, 0, 0, 1, 1, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }
};

uint8_t wink[8][12] = {
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0 },
{ 0, 0, 1, 1, 0, 0, 0, 0, 1, 1, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }
};

uint8_t cry[8][12] = {
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 1, 1, 0, 0, 0, 0, 1, 1, 0, 0 },
{ 0, 0, 1, 1, 0, 0, 0, 0, 1, 1, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }
};

Finally, call matrix.renderBitmap(smile, 8, 12); to load the array data into the LED matrix, and add an appropriate delay between each expression switch to display the expression.

Similarly, you can display Chinese characters, numbers, etc. by changing the corresponding array information.
Full code:
#include "Arduino_LED_Matrix.h"

ArduinoLEDMatrix matrix;

void setup()
{
Serial.begin(115200);
matrix.begin();
}

uint8_t smile[8][12] = {
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 1, 1, 0, 0, 0, 0, 1, 1, 0, 0 },
{ 0, 0, 1, 1, 0, 0, 0, 0, 1, 1, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }
};

uint8_t wink[8][12] = {
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0 },
{ 0, 0, 1, 1, 0, 0, 0, 0, 1, 1, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }
};

uint8_t cry[8][12] = {
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 1, 1, 0, 0, 0, 0, 1, 1, 0, 0 },
{ 0, 0, 1, 1, 0, 0, 0, 0, 1, 1, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }
};

void loop()
{
matrix.renderBitmap(smile, 8, 12);
delay(1000);
matrix.renderBitmap(wink, 8, 12);
delay(1000);
matrix.renderBitmap(cry, 8, 12);
delay(1000);
}

Video demonstration:
98a704f311287c9b4b56ff070b1cc1ae

DAC generates a sine wave
DAC is an analog-to-digital converter that can convert digital signals into analog signals. This function can be used to make a function generator and can also be used to drive a buzzer to play audio. Compared with PWM frequency control, it can achieve more continuous waveform generation, such as sine waves, sawtooth waves, etc.
First, the driver DAC can generate a stable voltage signal, and then change the input digital value each time to change the output voltage signal each time, so that the change of the voltage signal follows the change of the sinusoidal signal, and the generation of the sinusoidal signal can be realized. Let's implement it step by step.
By calling the function analogWriteResolution(8);, the resolution of the DAC is set to 8 bits. Then calling the function analogWrite(DAC, specific value (0-255)); can generate a stable DC voltage signal. We set the values to 100 and 150 respectively to see if the voltage changes. When it is 100, the data measured by the multimeter is 1.76V, and when it is 150, the data measured by the multimeter is 2.637V. When it is 255, which is the maximum number that can be set, the voltage value displayed by the voltmeter is 4.46. Therefore, it can be concluded that when the DAC is set to 8-bit mode, each value represents a voltage of approximately 0.01754V. According to the voltage value you want, you can get the value you want to enter by dividing it. For example, if you need a voltage value of 3V, 3/0.01754=171 (omitting the decimal place), enter 171, and you will get a voltage value of approximately 3V.
void setup() 
{
  analogWriteResolution(8);  // set the analog output resolution to 12 bit (4096 levels)
}

void loop() 
{
  analogWrite(DAC, 171);  // write the selected waveform on DAC0
}

Then we store the values of the generated sine signal in an array, pass each bit of the array to the function at a certain interval (related to the frequency), and use DAC to output the data into an analog signal to generate a sine wave. At the same time, we also find an array of sawtooth waves, but the sawtooth wave data is only 120 bits, and the DAC resolution is required to be 12, so we need to change the 8 in analogWriteResolution(8); to 12, change wave_sin in analogWrite(DAC, wave_sin); to waveformsTable, and change 256 in if (i == 256) to 120.

Waves.h Code
#ifndef _Waves_h_
#define _Waves_h_

static int waveformsTable[120] = {

    0x22, 0x44, 0x66, 0x88, 0xaa, 0xcc, 0xee, 0x110, 0x132, 0x154,
    0x176, 0x198, 0x1ba, 0x1dc, 0x1fe, 0x220, 0x242, 0x264, 0x286, 0x2a8,
    0x2ca, 0x2ec, 0x30e, 0x330, 0x352, 0x374, 0x396, 0x3b8, 0x3da, 0x3fc,
    0x41e, 0x440, 0x462, 0x484, 0x4a6, 0x4c8, 0x4ea, 0x50c, 0x52e, 0x550,
    0x572, 0x594, 0x5b6, 0x5d8, 0x5fa, 0x61c, 0x63e, 0x660, 0x682, 0x6a4,
    0x6c6, 0x6e8, 0x70a, 0x72c, 0x74e, 0x770, 0x792, 0x7b4, 0x7d6, 0x7f8,
    0x81a, 0x83c, 0x85e, 0x880, 0x8a2, 0x8c4, 0x8e6, 0x908, 0x92a, 0x94c,
    0x96e, 0x990, 0x9b2, 0x9d4, 0x9f6, 0xa18, 0xa3a, 0xa5c, 0xa7e, 0xaa0,
    0xac2, 0xae4, 0xb06, 0xb28, 0xb4a, 0xb6c, 0xb8e, 0xbb0, 0xbd2, 0xbf4,
    0xc16, 0xc38, 0xc5a, 0xc7c, 0xc9e, 0xcc0, 0xce2, 0xd04, 0xd26, 0xd48,
    0xd6a, 0xd8c, 0xdae, 0xdd0, 0xdf2, 0xe14, 0xe36, 0xe58, 0xe7a, 0xe9c,
    0xebe, 0xee0, 0xf02, 0xf24, 0xf46, 0xf68, 0xf8a, 0xfac, 0xfce, 0xff0
  

};

static int wave_sin[256] = {
  0x80,0x83,0x85,0x88,0x8A,0x8D,0x8F,0x92,   
  0x94,0x97,0x99,0x9B,0x9E,0xA0,0xA3,0xA5,   
  0xA7,0xAA,0xAC,0xAE,0xB1,0xB3,0xB5,0xB7,   
  0xB9,0xBB,0xBD,0xBF,0xC1,0xC3,0xC5,0xC7,   
  0xC9,0xCB,0xCC,0xCE,0xD0,0xD1,0xD3,0xD4,   
  0xD6,0xD7,0xD8,0xDA,0xDB,0xDC,0xDD,0xDE,   
  0xDF,0xE0,0xE1,0xE2,0xE3,0xE3,0xE4,0xE4,   
  0xE5,0xE5,0xE6,0xE6,0xE7,0xE7,0xE7,0xE7,   
  0xE7,0xE7,0xE7,0xE7,0xE6,0xE6,0xE5,0xE5,   
  0xE4,0xE4,0xE3,0xE3,0xE2,0xE1,0xE0,0xDF,   
  0xDE,0xDD,0xDC,0xDB,0xDA,0xD8,0xD7,0xD6,   
  0xD4,0xD3,0xD1,0xD0,0xCE,0xCC,0xCB,0xC9,   
  0xC7,0xC5,0xC3,0xC1,0xBF,0xBD,0xBB,0xB9,   
  0xB7,0xB5,0xB3,0xB1,0xAE,0xAC,0xAA,0xA7,   
  0xA5,0xA3,0xA0,0x9E,0x9B,0x99,0x97,0x94,   
  0x92,0x8F,0x8D,0x8A,0x88,0x85,0x83,0x80,   
  0x7D,0x7B,0x78,0x76,0x73,0x71,0x6E,0x6C,   
  0x69,0x67,0x65,0x62,0x60,0x5D,0x5B,0x59,   
  0x56,0x54,0x52,0x4F,0x4D,0x4B,0x49,0x47,   
  0x45,0x43,0x41,0x3F,0x3D,0x3B,0x39,0x37,   
  0x35,0x34,0x32,0x30,0x2E,0x2D,0x2C,0x2A,   
  0x29,0x28,0x26,0x25,0x24,0x23,0x22,0x21,   
  0x20,0x1F,0x1E,0x1D,0x1D,0x1C,0x1C,0x1B,   
  0x1B,0x1A,0x1A,0x1A,0x19,0x19,0x19,0x19,   
  0x19,0x19,0x19,0x19,0x1A,0x1A,0x1A,0x1B,   
  0x1B,0x1C,0x1C,0x1D,0x1D,0x1E,0x1F,0x20,   
  0x21,0x22,0x23,0x24,0x25,0x26,0x28,0x29,   
  0x2A,0x2C,0x2D,0x2F,0x30,0x32,0x34,0x35,   
  0x37,0x39,0x3B,0x3D,0x3F,0x41,0x43,0x45,   
  0x47,0x49,0x4B,0x4D,0x4F,0x52,0x54,0x56,   
  0x59,0x5B,0x5D,0x60,0x62,0x65,0x67,0x69,   
  0x6C,0x6E,0x71,0x73,0x76,0x78,0x7B,0x7D
};

#endif

Main function code

#include "Waves.h"

int i = 0;

void setup() 
{
  analogWriteResolution(8);  // set the analog output resolution to 12 bit (4096 levels)
}

void loop() 
{
  analogWrite(DAC, wave_sin[i]);  // write the selected waveform on DAC0
  i++;

  if (i == 256)  // Reset the counter to repeat the wave
    i = 0;
  delayMicroseconds(10);  // Hold the sample value for the sample time
}
3. Onboard Amplifier
The Arduino R4 WIFI board has a built-in amplifier, which can use A1 as the positive input, A2 as the reverse input, and A3 as the output. Therefore, we design a common-phase proportional amplifier with a magnification of twice to amplify the sinusoidal signal we generate.
To use the built-in amplifier, you must first import a library, #include <OPAMP.h> and then turn on the amplifier. Use the function OPAMP.begin(OPAMP_SPEED_HIGHSPEED); to turn on the amplifier.
In addition, the code analogWrite(DAC, wave_sin/2); only adds a division by 2. If it is not divided by 2, the amplified waveform will be clipped. Of course, it can also be adjusted by changing the value of the array. The rest is consistent with the DAC code, including the Waves.h library file.
#include "Waves.h"
#include <OPAMP.h>

int i = 0;

void setup() 
{
  OPAMP.begin(OPAMP_SPEED_HIGHSPEED);
  analogWriteResolution(8);  // set the analog output resolution to 12 bit (4096 levels)
}

void loop() 
{
  analogWrite(DAC, wave_sin[i]/2);  // write the selected waveform on DAC0
  i++;

  if (i == 256)  // Reset the counter to repeat the wave
    i = 0;
  delayMicroseconds(10);  // Hold the sample value for the sample time
}

The principle can be explained by this figure. The figure constitutes a simple in-phase proportional amplifier, in which the amplification factor is given by the yellow formula. The size of the two resistors can be adjusted to change the amplification factor. The red part is the main body of the amplifier circuit. A0 and A1 are short-circuited, that is, the analog signal generated by the DAC is input to the positive input terminal of the amplifier, and the output and the reverse input terminal form feedback. The blue part is a schematic of the waveform. The sine peak value of A1 input is 1V, and the sine peak value of A3 output is 2V.
The actual construction is relatively simple, but through the oscilloscope, it can be clearly seen that the output waveform has been significantly amplified compared to the input waveform, and the amplification factor is about 2 times.
4. ADC collects data and uses the serial port to print and draw waveforms.
ADC is an analog-to-digital converter, which works in the opposite way to DAC. It can convert the input analog signal into a digital signal and then process it digitally, which is more in line with the trend of digital development.
In terms of hardware connection, the A3 output pin of the amplifier needs to be connected to the ADC acquisition pin of A4.
To use ADC, you first need to initialize ADC and call the function analogReadResolution(8);. Then use the function int reading = analogRead(A4); to convert the analog value on the A4 pin into a digital value and store it in the reading variable. Then print the data and draw the waveform through the serial port.
#include "Waves.h"
#include <OPAMP.h>

int i = 0;

void setup() 
{
  OPAMP.begin(OPAMP_SPEED_HIGHSPEED);
  analogWriteResolution(8);  // set the analog output resolution to 12 bit (4096 levels)
  analogReadResolution(8); //change to 14-bit resolution
  Serial.begin(9600);
}

void loop() 
{
  analogWrite(DAC, wave_sin[i]/2);  // write the selected waveform on DAC0
  i++;

  if (i == 256)  // Reset the counter to repeat the wave
    i = 0;

  int reading = analogRead(A4); // returns a value between 0-16383

  delayMicroseconds(10);  // Hold the sample value for the sample time

  Serial.print(reading);
  Serial.print("\n");
}

This post is from DigiKey Technology Zone

Latest reply

The DAC, ADC and amplifier tests are OK, the waveforms are all out, thank you for sharing   Details Published on 2024-9-9 07:32
 
 

6570

Posts

0

Resources
2
 

The DAC, ADC and amplifier tests are OK, the waveforms are all out, thank you for sharing

This post is from DigiKey Technology Zone
 
 
 

Guess Your Favourite
Just looking around
Find a datasheet?

EEWorld Datasheet Technical Support

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京B2-20211791 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号
快速回复 返回顶部 Return list