stevie135sNeeraj Rane
Published © CC BY-SA

FM Radio with Clock Function

Clock radio using the RDA5807M and RTC DS1302

IntermediateProtip4,201
FM Radio with Clock Function

Things used in this project

Hardware components

RDA5807M FM Radio Module
×1
PAM8403 + Volume Control
×1
Arduino Nano
×1
Breadboard and hook up wire
×1
rtc ds1302 module
×1
SSD1306 OLED Display
×1

Software apps and online services

Arduino IDE
Arduino IDE

Story

Read more

Custom parts and enclosures

Radio Housing

3D Printer STL

Radio Fascia

3D Printer STL

Schematics

Clock Radio Wiring Diagram

Vero Schematic

Veroboard design. Make sure the data tracks are short to prevent cross track capacitance interference.

Code

Clock Radio Final

Arduino
#include <Adafruit_SSD1306.h>
#include <Wire.h>
#include <RDA5807M.h>
#include <Arduino.h>
#include <Ds1302.h>
#include <LowPower.h>
#include <SPI.h>
#define OLED_RESET -1
#define FIX_BAND RADIO_BAND_FM   
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 32 // OLED display heightint setButton = 9;
Ds1302 rtc(8,6,7); // RST , CLK , DAT

int dsplyButton = 11;
int setButton = 9;
int backButton = 10;
int potPin = A0;


float displayStation;
int stationNum, val;

Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);
RDA5807M radio;

// Select Your own radio frequencies for your area
int stations[10] = {9110,9370,9580,9670,9740,9890,10040,10540,10590,10760};
const static char* WeekDays[] =
{
    "Monday    ",
    "Tuesday   ",
    "Wednesday ",
    "Thursday  ",
    "Friday    ",
    "Saturday  ",
    "Sunday    "
};
const static char* MonthText[] =
{
    "Jan",
    "Feb",
    "Mar",
    "Apr",
    "May",
    "Jun",
    "Jul",
    "Aug",
    "Sep",
    "Oct",
    "Nov",
    "Dec"
};

void setup() {

  rtc.init();
  pinMode(dsplyButton,INPUT_PULLUP);
  pinMode(setButton,INPUT_PULLUP);
  pinMode(backButton, INPUT_PULLUP);
  display.begin(SSD1306_SWITCHCAPVCC, 0x3C); 
  display.display();
  delay(2000);
  display.clearDisplay();
    
  radio.init();
  radio.setBandFrequency(FIX_BAND, 9110);
  radio.setVolume(8);
  radio.setMono(false);
  radio.setMute(false);
  display.setTextSize(3);
  display.setTextColor(WHITE);
  display.setCursor(20,10);
  display.print("91.10");
  display.display();
}

void loop() {
    radio.setVolume(8);
    Ds1302::DateTime now;
    rtc.getDateTime(&now);
    static uint8_t last_second = 0;
  

       if (last_second != now.second)
    {
        last_second = now.second;
        display.setTextColor(SSD1306_WHITE);
        display.setTextSize(1);
        display.setCursor(40,0);
        if (now.hour <= 9) { //If Hour is single figures, put a 0 in front
        display.print("0");
        }
        display.print(now.hour);
        display.print(":");
     if (now.minute <= 9) {  //If Minute is single figures, put a 0 in front
        display.print("0");
     }
        display.print(now.minute);
        display.print(":"); 
        if (now.second <= 9) {  //If Seconds is single figures, put a 0 in front
        display.print("0");
     } 
        display.print(now.second);

        display.display();
                       
        display.setTextColor(SSD1306_BLACK);
        display.setCursor(40,0);
     if (now.hour <= 9) {
        display.print("0");
    }
       display.print(now.hour);
       display.print(":");
     if (now.minute <= 9) {
       display.print("0");
  }
       display.print(now.minute);
       display.print(":");
     if (now.second <= 9) {
       display.print("0");
  } 
      display.print(now.second); 
  }

    if (digitalRead(backButton)== LOW) {
  
    setFrequency();
}
if (digitalRead(dsplyButton) == LOW) {

  while (digitalRead(dsplyButton) == LOW) {
    
  }

    radio.term();
    DplyClock();

}



}
void setFrequency(){
  display.setTextColor(SSD1306_WHITE);

//  radio.setMute(false);
  while(digitalRead(setButton) == HIGH){
    display.setTextSize(1);
    display.setTextColor(WHITE);
    display.setCursor(0,0);
    display.print("Select Station");
    display.setTextSize(3);
    display.setTextColor(WHITE);
    display.setCursor(20,10);
    val = analogRead(potPin);
    stationNum = map(val, 0, 1023, 0, 9);
    displayStation = stations[stationNum]/100.0;
    display.print(displayStation);
    display.display();
    display.clearDisplay();
  }
  delay(500);
  radio.setBandFrequency(FIX_BAND, stations[stationNum]);
  display.setCursor(20,10);
  display.print(displayStation);
  display.display();
}


void DplyClock() {


    display.clearDisplay();
    while (digitalRead(dsplyButton) == HIGH) {
  
      // get the current time
    Ds1302::DateTime now;
    rtc.getDateTime(&now);
    static uint8_t last_second = 0;
    if (last_second != now.second)
    {
        last_second = now.second;
        display.setTextColor(SSD1306_WHITE);
        display.setTextSize(2);
        display.setCursor(15,2);
        if (now.hour <= 9) { //If Hour is single figures, put a 0 in front
        display.print("0");
        }
        display.print(now.hour);
        display.print(":");
     if (now.minute <= 9) {  //If Minute is single figures, put a 0 in front
        display.print("0");
     }
        display.print(now.minute);
        display.print(":"); 
        if (now.second <= 9) {  //If Seconds is single figures, put a 0 in front
        display.print("0");
     } 
        display.print(now.second);

        display.setTextSize(1);
        display.setCursor(17,22);
        display.print(WeekDays[now.dow -1]);
        display.print(now.day);
        display.print(" ");
        display.print(MonthText[now.month -1]);
        
        display.display();

        
        display.setTextColor(SSD1306_BLACK);
        display.setCursor(17,22);
        display.print(WeekDays[now.dow -1]);
        display.print(now.day);
        display.print(" ");
        display.print(MonthText[now.month -1]);           
        
                
        
        display.setTextSize(2);
        display.setCursor(15,2);
     if (now.hour <= 9) {
        display.print("0");
    }
       display.print(now.hour);
       display.print(":");
     if (now.minute <= 9) {
       display.print("0");
  }
       display.print(now.minute);
       display.print(":");
     if (now.second <= 9) {
       display.print("0");
  } 
      display.print(now.second);
  
}
    LowPower.powerDown(SLEEP_250MS,ADC_OFF,BOD_OFF);  // Sleep for 250MS/Sec in Low Power
}
while (digitalRead(dsplyButton) == LOW){
  }
  display.setTextColor(SSD1306_WHITE);
  radio.init();
  radio.setBandFrequency(FIX_BAND, stations[stationNum]);
  display.clearDisplay();
  display.setTextSize(3);
  display.setCursor(20,10);
  stationNum = map(val, 0, 1023, 0, 9);
  displayStation = stations[stationNum]/100.0;
  display.print(displayStation);
  display.display();
}

Credits

stevie135s

stevie135s

25 projects • 11 followers
Neeraj Rane

Neeraj Rane

18 projects • 47 followers
Electrical Engineer and a Maker from India. Engineering is fun once you start applying it!

Comments