Wemos buzzer shield example

In this example we look at another Wemos mini shield, this time its the buzzer_shield which as its name suggests is a basic buzzer. Its an MLT_8540.

The product page is https://wiki.wemos.cc/products:d1_mini_shields:buzzer_shield

 

Code

This example plays a few notes from a popular song

int buzzerPin=D5; //Buzzer control port, default D5
const int songLength = 18;
char notes[] = "cdfda ag cdfdg gf ";
int beats[] = {1,1,1,1,1,1,4,4,2,1,1,1,1,1,1,4,4,2};
int tempo = 150;
 
 
void setup() 
{
  pinMode(buzzerPin, OUTPUT);
}
 
 
void loop() 
{
  int i, duration;
 
  for (i = 0; i < songLength; i++) // step through the song arrays
  {
    duration = beats[i] * tempo;  // length of note/rest in ms
 
    if (notes[i] == ' ')          // is this a rest? 
    {
      delay(duration);            // then pause for a moment
    }
    else                          // otherwise, play the note
    {
      tone(buzzerPin, frequency(notes[i]), duration);
      delay(duration); // wait for tone to finish
    }
    delay(tempo/10);              // brief pause between notes
  }
 
  while(true){}
 
}
 
 
int frequency(char note) 
{
 
  int i;
  const int numNotes = 8;  // number of notes we're storing
  char names[] = { 'c', 'd', 'e', 'f', 'g', 'a', 'b', 'C' };
  int frequencies[] = {262, 294, 330, 349, 392, 440, 494, 523};
 
 
  for (i = 0; i < numNotes; i++)  // Step through the notes
  {
    if (names[i] == note)         // Is this the one?
    {
      return(frequencies[i]);     // Yes! Return the frequency
    }
  }
  return(0); 
}

Wemos mini MATRIX LED Shield example

A new shield for the Wemos Mini came my way recently, this is called the MATRIX LED Shield – You can find out more at https://wiki.wemos.cc/products:d1_mini_shields:matrix_led_shield

The board uses a TM1640, as you can see there are 8×8 LEDs with 8 levels of intensity. The shield will also use the D5 an D7 pins of your wemos mini, so these won’t be available for any other projects

Code

This requires the library from https://github.com/wemos/WEMOS_Matrix_LED_Shield_Arduino_Library

There are a couple of built in examples but here are a few examples that will flash various LEDs

#include <WEMOS_Matrix_LED.h>
 
MLED mled(5); //set intensity=5
 
void setup()
{
 
}
 
void loop() {
 
  for(int y=0;y<8;y++)
  {
    for(int x=0;x<8;x++)
    {
        mled.dot(x,y); // draw dot
        mled.display();
        delay(200);      
    }  
  }
}
#include <WEMOS_Matrix_LED.h>
MLED mled(5); //set intensity=5
void setup()
{
}
void loop() {
for(int y=0;y<8;y++)
{
mled.dot(0,y); // draw dot
mled.display();
delay(200);
}
}

#include <WEMOS_Matrix_LED.h>
MLED mled(5); //set intensity=5
int randX, randY;
void setup()
{
Serial.begin(9600);
randomSeed(analogRead(0));
}
void loop()
{
randX = random(8);
randY = random(8);
mled.dot(randX,randY); // draw dot
mled.display();
delay(200);
}

#include <WEMOS_Matrix_LED.h>
MLED mled(5); //set intensity=5
int randX, randY;
void setup()
{
Serial.begin(9600);
randomSeed(analogRead(0));
}
void loop()
{
randX = random(8);
randY = random(8);
mled.dot(randX,randY); // draw dot
mled.display();
delay(200);
mled.dot(randX,randY,0);//clear dot
mled.display();
delay(200);
}

 

Links

 

Wemos and DS3231 RTC example

The DS3231 is a low-cost, extremely accurate I2C real-time clock (RTC) with an integrated temperature-compensated crystal oscillator (TCXO) and crystal.
The device incorporates a battery input, and maintains accurate timekeeping when main power to the device is interrupted.
Features

Highly Accurate RTC Completely Manages All Timekeeping Functions
Real-Time Clock Counts Seconds, Minutes, Hours, Date of the Month, Month, Day of the Week, and Year, with Leap-Year Compensation Valid Up to 2100
Accuracy ±2ppm from 0°C to +40°C
Accuracy ±3.5ppm from -40°C to +85°C
Digital Temp Sensor Output: ±3°C Accuracy
Two Time-of-Day Alarms
Programmable Square-Wave Output Signal
Simple Serial Interface Connects to Most Microcontrollers
Fast (400kHz) I2C Interface
Battery-Backup Input for Continuous Timekeeping
Low Power Operation Extends Battery-Backup Run Time
3.3V Operation

This is a very common module that can be bought

Here is a schematic

 

Connection

 Wemos Pins Module Pins 
 GND  GND
3v3  VCC
D2  SDA
D1  SCL

 

Code

This uses the RTCLib – https://github.com/adafruit/RTClib

#include <Wire.h>
#include "RTClib.h"
 
RTC_DS3231 rtc;
 
char daysOfTheWeek[7][12] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};
 
void setup () {
 
#ifndef ESP8266
  while (!Serial); // for Leonardo/Micro/Zero
#endif
 
  Serial.begin(9600);
 
  delay(3000); // wait for console opening
 
  if (! rtc.begin()) {
    Serial.println("Couldn't find RTC");
    while (1);
  }
 
  if (rtc.lostPower()) {
    Serial.println("RTC lost power, lets set the time!");
    // following line sets the RTC to the date & time this sketch was compiled
    rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
    // This line sets the RTC with an explicit date & time, for example to set
    // January 21, 2014 at 3am you would call:
    // rtc.adjust(DateTime(2014, 1, 21, 3, 0, 0));
  }
}
 
void loop () {
    DateTime now = rtc.now();
 
    Serial.print(now.year(), DEC);
    Serial.print('/');
    Serial.print(now.month(), DEC);
    Serial.print('/');
    Serial.print(now.day(), DEC);
    Serial.print(" (");
    Serial.print(daysOfTheWeek[now.dayOfTheWeek()]);
    Serial.print(") ");
    Serial.print(now.hour(), DEC);
    Serial.print(':');
    Serial.print(now.minute(), DEC);
    Serial.print(':');
    Serial.print(now.second(), DEC);
    Serial.println();
 
    Serial.print(" since midnight 1/1/1970 = ");
    Serial.print(now.unixtime());
    Serial.print("s = ");
    Serial.print(now.unixtime() / 86400L);
    Serial.println("d");
 
    // calculate a date which is 7 days and 30 seconds into the future
    DateTime future (now + TimeSpan(7,12,30,6));
 
    Serial.print(" now + 7d + 30s: ");
    Serial.print(future.year(), DEC);
    Serial.print('/');
    Serial.print(future.month(), DEC);
    Serial.print('/');
    Serial.print(future.day(), DEC);
    Serial.print(' ');
    Serial.print(future.hour(), DEC);
    Serial.print(':');
    Serial.print(future.minute(), DEC);
    Serial.print(':');
    Serial.print(future.second(), DEC);
    Serial.println();
 
    Serial.println();
    delay(3000);
}

Links
DS3231 AT24C32 IIC Module Precision Clock Module

Wemos and PCF8563 RTC example

The PCF8563 is a CMOS Real-Time Clock (RTC) and calendar optimized for low power consumption. A programmable clock output, interrupt output, and voltage-low detector are also provided. All addresses and data are transferred serially via a two-line bidirectional I²C-bus. Maximum bus speed is 400 kbit/s.

Features

  • Provides year, month, day, weekday, hours, minutes, and seconds based on a 32.768 kHz quartz crystal
  • Century flag
  • Clock operating voltage: 1.0 V to 5.5 V at room temperature
  • Low backup current; typical 0.25 μA at VDD = 3.0 V and Tamb = 25 °C
  • 400 kHz two-wire I²C-bus interface (at VDD = 1.8 V to 5.5 V)
  • Programmable clock output for peripheral devices (32.768 kHz, 1.024 kHz, 32 Hz, and 1 Hz)
  • Alarm and timer functions
  • Integrated oscillator capacitor
  • Internal Power-On Reset (POR)
  • I²C-bus slave address: read A3h and write A2h
  • Open-drain interrupt pin

I bought the folllowing module which is made by Waveshare

This is the schematic for the module, you could easily build this module on a breadboard

Here is how to connect the module to your Arduino

 

Connection

Wemos Pin Module Pin
 5v  Vcc
GND Gnd
D1 SCL
D2 SDA

 

Code

You do not need a library but I downloaded the library from https://bitbucket.org/orbitalair/arduino_rtc_pcf8563/downloads

This is one of the basic examples, I tested this on a Wemos Mini and Arduino and the results were identical

/* Demonstration of Rtc_Pcf8563 Alarms. 
 *
 * The Pcf8563 has an interrupt output, Pin3.
 * Pull Pin3 HIGH with a resistor, I used a 10kohm to 5v.
 * I used a RBBB with Arduino IDE, the pins are mapped a 
 * bit differently.  Change for your hw.
 * SCK - A5, SDA - A4, INT - D3/INT1
 *
 * After loading and starting the sketch, use the serial monitor
 * to see the clock output.
 * 
 * setup:  see Pcf8563 data sheet.
 *         1x 10Kohm pullup on Pin3 INT
 *         No pullups on Pin5 or Pin6 (I2C internals used)
 *         1x 0.1pf on power
 *         1x 32khz chrystal
 *
 * Joe Robertson, jmr
 * orbitalair@bellsouth.net
 */
#include <Wire.h>
#include <Rtc_Pcf8563.h>
 
/* get a real time clock object */
Rtc_Pcf8563 rtc;
/* a flag for the interrupt */
volatile int alarm_flag=0;
 
/* the interrupt service routine */
void blink()
{
  alarm_flag=1;
}
 
void setup()
{
  pinMode(3, INPUT);           // set pin to input
  digitalWrite(3, HIGH);       // turn on pullup resistors
 
  Serial.begin(9600);
 
  /* setup int on pin 3 of arduino */
  attachInterrupt(1, blink, FALLING);
  /* clear out all the registers */
  rtc.initClock();
  /* set a time to start with.
   * day, weekday, month, century, year */
  rtc.setDate(14, 6, 3, 0, 10);
  /* hr, min, sec */
  rtc.setTime(1, 15, 40);
  /* set an alarm for 20 secs later...
   * alarm pin goes low when match occurs
   * this triggers the interrupt routine
   * min, hr, day, weekday 
   * 99 = no alarm value to be set
   */
  rtc.setAlarm(16, 99, 99, 99);
}
 
void loop()
{
  /* each sec update the display */
  Serial.print(rtc.formatTime());
  Serial.print("  ");
  Serial.print(rtc.formatDate());
  Serial.print("  0x");
  Serial.print(rtc.getStatus2(), HEX);
  Serial.print("\r\n");
  delay(1000);
  if (alarm_flag==1){
    clr_alarm();
  }
 
}
 
void clr_alarm()
{
  detachInterrupt(1);
  Serial.print("blink!\r\n");
 
  rtc.clearAlarm();
  delay(1000);
  alarm_flag=0;
  attachInterrupt(1, blink, FALLING);
}

Links
PCF8563 RTC Board PCF8563T CMOS Real-time Clock/Calendar Development Module