【雕爷学编程】Arduino动手做(136)---0.91寸OLED液晶屏模块4

37款传感器与执行器的提法,在网络上广泛流传,其实Arduino能够兼容的传感器模块肯定是不止这37种的。鉴于本人手头积累了一些传感器和执行器模块,依照实践出真知(一定要动手做)的理念,以学习和交流为目的,这里准备逐一动手尝试系列实验,不管成功(程序走通)与否,都会记录下来—小小的进步或是搞不掂的问题,希望能够抛砖引玉。

【Arduino】168种传感器模块系列实验(资料代码+仿真编程+图形编程)
实验一百三十六:0.91寸OLED液晶屏显示模块 IIC 12832液晶屏 兼容3.3v-5V

在这里插入图片描述
实验程序之八:识别指纹ID,OLED显示识别人名

Arduino实验开源代码

/*

 【Arduino】168种传感器模块系列实验(资料代码+仿真编程+图形编程)

   实验一百三十六:0.91寸OLED液晶屏显示模块 IIC 12832液晶屏 兼容3.3v-5V

  安装AS608库:IDE—工具—管理库—搜索Adafruit-Fingerprint-Sensor-Library—安装

  安装OLED库:IDE—工具—管理库—搜索Adafruit_SSD1306—安装

  安装OLED库:IDE—工具—管理库—搜索Adafruit_GFX—安装

  实验程序之十三:识别指纹ID,OLED显示识别人名

  AS608模块实验接线

  Vi    +3.3V(请勿接3.3V以上电源,否则烧毁模块!)

  TX    2

  RX    3

  GND  GND

  OLED 屏幕实验接线

  oled模块  Ardunio Uno

  GND---------GND接地线

  VCC---------5V 接电源

  SDA---------A4

  SCL ------- A5

*/



#include <Wire.h>

#include <Adafruit_GFX.h>

#include <Adafruit_SSD1306.h>

#define OLED_RESET 4

Adafruit_SSD1306 display(OLED_RESET);

#include <Adafruit_Fingerprint.h>

#include <SoftwareSerial.h>

SoftwareSerial mySerial(2, 3);

Adafruit_Fingerprint finger = Adafruit_Fingerprint(&mySerial);

int fingerprintID = 0;

String IDname;

void setup() {
    
    

  //Fingerprint sensor module setup

  Serial.begin(9600);

  // set the data rate for the sensor serial port

  finger.begin(57600);

  if (finger.verifyPassword()) {
    
    

  Serial.println("Found fingerprint sensor!");

  }

  else {
    
    

  Serial.println("Did not find fingerprint sensor ");

  while (1) {
    
    

    delay(1);

  }

  }

  //OLED display setup

  Wire.begin();

  display.begin(SSD1306_SWITCHCAPVCC, 0x3C);

  //displays main screen

  displayMainScreen();

}

void loop() {
    
    

  displayMainScreen();

  fingerprintID = getFingerprintIDez();

  delay(50);

  if (fingerprintID == 1 || fingerprintID == 3) {
    
    

  IDname = "huadiao";

  displayUserGreeting(IDname);

  }

  else if (fingerprintID == 2) {
    
    

  IDname = "zhanghaoze";

  displayUserGreeting(IDname);

  }

}

// returns -1 if failed, otherwise returns ID #

int getFingerprintIDez() {
    
    

  uint8_t p = finger.getImage();

  if (p != FINGERPRINT_OK)  return -1;

  p = finger.image2Tz();

  if (p != FINGERPRINT_OK)  return -1;

  p = finger.fingerFastSearch();

  if (p != FINGERPRINT_OK)  return -1;

  // found a match!

  Serial.print("Found ID #");

  Serial.print(finger.fingerID);

  Serial.print(" with confidence of ");

  Serial.println(finger.confidence);

  return finger.fingerID;

}

void displayMainScreen() {
    
    

  display.clearDisplay();

  display.setTextSize(1);

  display.setTextColor(WHITE);

  display.setCursor(7, 5);

  display.println("Waiting fingerprint");

  display.setTextSize(1);

  display.setTextColor(WHITE);

  display.setCursor(52, 20);

  display.println("...");

  display.display();

  delay(2000);

}

void displayUserGreeting(String Name) {
    
    

  display.clearDisplay();

  display.setTextColor(WHITE);

  display.setTextSize(2);

  display.setCursor(0, 0);

  display.print("Hello");

  display.setCursor(0, 15);

  display.print(Name);

  display.display();

  delay(5000);

  fingerprintID = 0;

}

本实验案例中,通过程序之二enroll,注册了“huadaio”的二个指纹1#和3号,注册了“zhanghaoze”一个指纹2#。识别指纹ID后,在串口显示匹配的信心值,在OLED屏显示匹配的招呼语。

Found fingerprint sensor!

找到指纹传感器!

在这里插入图片描述
OLED显示

Waiting fingerprint

等待指纹

在这里插入图片描述

串口显示识别的三个指纹ID

在这里插入图片描述
OLED屏显示识别1#指纹“huadaio”

在这里插入图片描述
OLED屏显示识别2#指纹“zhanghaoze”

在这里插入图片描述
MAX9814 麦克风放大器

MAX9814 是一款麦克风放大器,具有自动增益控制 (AGC) 和低噪声麦克风偏置功能,并且有几个选项可以通过突破进行配置。

MAX9814 模块具有三种放大器设置(40dB、50dB、60dB)。默认值为 60dB,但可以通过将增益引脚跳接到 VCC 或接地来设置为 40dB 或 50dB。

内部低噪声麦克风偏置 1.25V,2Vpp。放大器的输出在 1.25V 直流偏置时最大约为 2Vpp,因此它可以轻松用于任何高达 3.3V 输入的模数转换器。

Attack/Release 比率也可以修改,从默认的 1:4000 到 1:2000 或 1:500。

要直接连接到线路输入,您需要一个隔直电容/耦合电容来隔断直流分量。您可以使用 1uF 以上的串联阻塞电容器,这取决于您的系统,如果有疑问,请尝试从 33 uF 到 100uF。耦合电路允许交流信号从一个部分流到另一个部分,同时阻止直流分量。在音频电路中,这样做是为了防止直流分量使音频输出失真。耦合电容器的有效性取决于一系列广泛的频率相关参数,包括插入损耗、等效串联电阻和串联谐振频率。

电源:2.7V 至 5.5V

在这里插入图片描述
【Arduino】168种传感器模块系列实验(资料代码+仿真编程+图形编程)

实验一百三十六:0.91寸OLED液晶屏显示模块 IIC 12832液晶屏 兼容3.3v-5V

项目十六:使用FFT库的迷你音乐频谱仪(声谱可视化器)

实验接线方法: max9814接A0

oled模块 Ardunio Uno

GND---------GND接地线

VCC---------5V 接电源

SDA---------A4

SCL ------- A5

/*

 【Arduino】168种传感器模块系列实验(资料代码+仿真编程+图形编程)

  实验一百三十六:0.91寸OLED液晶屏显示模块 IIC 12832液晶屏 兼容3.3v-5V

 项目十六:使用FFT库的迷你音乐频谱仪(声谱可视化器)

 实验接线方法: max9814接A0

 oled模块  Ardunio Uno

 GND---------GND接地线

 VCC---------5V 接电源

 SDA---------A4

 SCL ------- A5

*/

#include "arduinoFFT.h"

#include <Adafruit_GFX.h>

#include <Adafruit_SSD1306.h>

#define SAMPLES 64 // power of 2

#define SAMPLING_FREQ 8000 // 12 kHz Fmax = sampleF /2 

#define AMPLITUDE 100 // 灵敏度

#define FREQUENCY_BANDS 14

#define SCREEN_WIDTH 128

#define SCREEN_HEIGHT 32

#define BARWIDTH 11

#define BARS 11

#define ANALOG_PIN A0

#define OLED_RESET   -1 // 重置引脚 #(如果共享 Arduino 重置引脚,则为 -1)

Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);

double vImag[SAMPLES];

double vReal[SAMPLES];

unsigned long sampling_period_us;

arduinoFFT fft = arduinoFFT(vReal, vImag, SAMPLES, SAMPLING_FREQ);

//调整参考以去除背景噪声

float reference = log10(60.0);

double coutoffFrequencies[FREQUENCY_BANDS];

void setup() {
    
    

 // SSD1306_SWITCHCAPVCC = generate display voltage from 3.3V internally

 if (!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
    
     // Address 0x3C for 128x32

  for (;;); // Don't proceed, loop forever

 }

 // Setup display

 display.clearDisplay();

 display.display();

 display.setRotation(0);

 display.invertDisplay(false);

 sampling_period_us = (1.0 / SAMPLING_FREQ ) * pow(10.0, 6);

 // 计算截止频率,以对数标度为基数 POt

 double basePot = pow(SAMPLING_FREQ / 2.0, 1.0 / FREQUENCY_BANDS);

 coutoffFrequencies[0] = basePot;

 for (int i = 1 ; i < FREQUENCY_BANDS; i++ ) {
    
    

  coutoffFrequencies[i] = basePot * coutoffFrequencies[i - 1];

 }

 // 绘制虚线以分离频段

 for (int i = 0; i < BARS - 1 ; i++) {
    
    

  for (int j = 0; j < SCREEN_HEIGHT ; j += 4) {
    
    

   display.writePixel((i + 1)*BARWIDTH + 2 , j, SSD1306_WHITE );

  }

 }

 display.drawRect(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT, SSD1306_WHITE);

}

int oldHeight[20];

int oldMax[20];

double maxInFreq;

void loop() {
    
    

 // 采样

 for (int i = 0; i < SAMPLES; i++) {
    
    

  unsigned long newTime = micros();

  int value = analogRead(ANALOG_PIN);

  vReal[i] = value;

  vImag[i] = 0;

  while (micros() < (newTime + sampling_period_us)) {
    
    

   yield();

  }

 }

 // 计算 FFT

 fft.DCRemoval();

 fft.Windowing(FFT_WIN_TYP_HAMMING, FFT_FORWARD);

 fft.Compute(FFT_FORWARD);

 fft.ComplexToMagnitude();

 double median[20];

 double max[20];

 int index = 0;

 double hzPerSample = (1.0 * SAMPLING_FREQ) / SAMPLES; //

 double hz = 0;

 double maxinband = 0;

 double sum = 0;

 int count = 0;

 for (int i = 2; i < (SAMPLES / 2) ; i++) {
    
    

  count++;

  sum += vReal[i];

  if (vReal[i] > max[index] ) {
    
    

   max[index] = vReal[i];

  }

  if (hz > coutoffFrequencies[index]) {
    
    

   median[index] = sum / count;

   sum = 0.0;

   count = 0;

   index++;

   max[index] = 0;

   median[index] = 0;

  }

  hz += hzPerSample;

 }

 // 计算每个频段的中值和最大值

 if ( sum > 0.0) {
    
    

  median[index] = sum / count;

  if (median[index] > maxinband) {
    
    

   maxinband = median[index];

  }

 }

 int bar = 0;

 for (int i = FREQUENCY_BANDS - 1; i >= 3; i--) {
    
    

  int newHeight = 0;

  int newMax = 0;

  // 计算实际分贝

  if (median[i] > 0 && max[i] > 0 ) {
    
    

   newHeight = 20.0 * (log10(median[i] ) - reference);

   newMax = 20.0 * (log10(max[i] ) - reference);

  }

  // 调整最小和最大级别

  if (newHeight < 0 || newMax < 0) {
    
    

   newHeight = 1;

   newMax = 1;

  }

  if (newHeight >= SCREEN_HEIGHT - 2) {
    
    

   newHeight = SCREEN_HEIGHT - 3;

  }

  if (newMax >= SCREEN_HEIGHT - 2) {
    
    

   newMax = SCREEN_HEIGHT - 3;

  }

  int barX = bar * BARWIDTH + 5;

  // 删除旧水平中位数

  if (oldHeight[i] > newHeight) {
    
    

   display.fillRect(barX, newHeight + 1, 7, oldHeight[i], SSD1306_BLACK);

  }

  // 删除旧的最大级别

  if ( oldMax[i] > newHeight) {
    
    

   for (int j = oldMax[i]; j > newHeight; j -= 2) {
    
    

    display.drawFastHLine(barX , j, 7, SSD1306_BLACK);

   }

  }

  // 绘制新的最大级别

  for (int j = newMax; j > newHeight; j -= 2) {
    
    

   display.drawFastHLine(barX , j, 7, SSD1306_WHITE);

  }

  // 绘制新的级别中位数

  display.fillRect(barX , 1, 7, newHeight, SSD1306_WHITE);

  oldMax[i] = newMax;

  oldHeight[i] = newHeight;

  bar++;

 }

 display.drawFastHLine(0 , SCREEN_HEIGHT - 1, SCREEN_WIDTH, SSD1306_WHITE);

 display.display();

}


Arduino实验场景图

在这里插入图片描述
【Arduino】168种传感器模块系列实验(资料代码+仿真编程+图形编程)

实验一百三十六:0.91寸OLED液晶屏显示模块 IIC 12832液晶屏 兼容3.3v-5V

项目十六:使用FFT库的迷你音乐频谱仪(声谱可视化器)(视频67秒)

https://v.youku.com/v_show/id_XNTgwNzU4NDQwOA==.html?spm=a2hcb.playlsit.page.1

项目十六:使用FFT库的迷你音乐频谱仪(声谱可视化器)(测试视频113秒)

https://v.youku.com/v_show/id_XNTgwNzYwODA2OA==.html?spm=a2hcb.playlsit.page.1

终于找到黄色的,收了二片
在这里插入图片描述
【Arduino】168种传感器模块系列实验(资料代码+仿真编程+图形编程)

实验一百三十六:0.91寸OLED液晶屏显示模块 IIC 12832液晶屏 兼容3.3v-5V

项目十七:综合测试,中文显示四个字

实验开源代码

/*

 【Arduino】168种传感器模块系列实验(资料代码+仿真编程+图形编程)

 实验一百三十六:0.91寸OLED液晶屏显示模块 IIC 12832液晶屏 兼容3.3v-5V

 项目十七:综合测试,中文显示四个字

 实验接线方法: max9814接A0

 oled模块  Ardunio Uno

 GND---------GND接地线

 VCC---------5V 接电源

 SDA---------A4

 SCL ------- A5

*/

#include <Wire.h>

#include <Adafruit_GFX.h>

#include <Adafruit_SSD1306.h>

#define OLED_RESET   4

Adafruit_SSD1306 display(128, 32, &Wire, OLED_RESET);

//取16X16汉字字模 逐行式 顺向高位在前

static const unsigned char PROGMEM str1[] =

{
    
    

 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x3F, 0xF8, 0x21, 0x08, 0x21, 0x08, 0x21, 0x08,

 0x21, 0x08, 0x21, 0x08, 0x3F, 0xF8, 0x21, 0x08, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00

};/*"中",0*/

static const unsigned char PROGMEM str2[] =

{
    
    

 0x02, 0x00, 0x01, 0x00, 0x01, 0x00, 0xFF, 0xFE, 0x10, 0x10, 0x10, 0x10, 0x08, 0x20, 0x08, 0x20,

 0x04, 0x40, 0x02, 0x80, 0x01, 0x00, 0x02, 0x80, 0x04, 0x40, 0x08, 0x20, 0x30, 0x18, 0xC0, 0x06

};/*"文",1*/

static const unsigned char PROGMEM str3[] =

{
    
    

 0x00, 0x00, 0x1F, 0xF0, 0x10, 0x10, 0x10, 0x10, 0x1F, 0xF0, 0x10, 0x10, 0x10, 0x10, 0x1F, 0xF0,

 0x04, 0x40, 0x44, 0x44, 0x24, 0x44, 0x14, 0x48, 0x14, 0x50, 0x04, 0x40, 0xFF, 0xFE, 0x00, 0x00

};/*"显",2*/

static const unsigned char PROGMEM str4[] =

{
    
    

 0x00, 0x00, 0x3F, 0xF8, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFF, 0xFE, 0x01, 0x00,

 0x01, 0x00, 0x11, 0x10, 0x11, 0x08, 0x21, 0x04, 0x41, 0x02, 0x81, 0x02, 0x05, 0x00, 0x02, 0x00

};/*"示",3*/

void setup() {
    
    

 display.begin(SSD1306_SWITCHCAPVCC, 0x3C);

 display.setTextColor(WHITE);//开像素点发光

 display.clearDisplay();//清屏

 display.setTextSize(2); //设置字体大小

 display.setCursor(0, 0);//设置显示位置

 display.println("-TonyCode-");//输出字符

 display.drawBitmap(32, 16, str1, 16, 16, 1); //画出字符对应点阵数据

 display.drawBitmap(48, 16, str2, 16, 16, 1); //画出字符对应点阵数据

 display.drawBitmap(64, 16, str3, 16, 16, 1); //画出字符对应点阵数据

 display.drawBitmap(80, 16, str4, 16, 16, 1); //画出字符对应点阵数据

 display.display();//打开显示

}

void loop() {
    
    

}

Arduino实验场景图

在这里插入图片描述
【Arduino】168种传感器模块系列实验(资料代码+仿真编程+图形编程)

实验一百三十六:0.91寸OLED液晶屏显示模块 IIC 12832液晶屏 兼容3.3v-5V

项目十八:简单的流动文本显示

实验开源代码

/*

 【Arduino】168种传感器模块系列实验(资料代码+仿真编程+图形编程)

  实验一百三十六:0.91寸OLED液晶屏显示模块 IIC 12832液晶屏 兼容3.3v-5V

 项目十八:简单的流动文本显示

 实验接线方法: 

 oled模块  Ardunio Uno

 GND---------GND接地线

 VCC---------5V 接电源

 SDA---------A4

 SCL ------- A5

*/



#include<FLOW.h>

FLOW first;

String s="Hello Eagler8!";



void setup(){
    
    

 first.Begin();

}



void loop(){
    
    

 first.Now(24,8,s,1);

 first.Clear(50);

}

Arduino实验场景图

在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/weixin_41659040/article/details/131591612