Help with TM1637Display Coding

So I am new Arduino, while I have many years of experience with electronics and computer hardware experience, my understanding of programing languages is highly limited. The most I ever did was using BASIC back in the 1980’s. While I can fuddle my way through some of it, or sometimes edit existing code to achieve a result, I don’t have a good enough understanding of the basics yet to do much on my own. It’s one of the reasons I am playing around with Arduino, to try and start to learn some C basics.

In this case, I am working on a simple project for my wife. She’s really into “paranormal” things and decided to put together a simple “spirit box” for her. A device to sweep radio frequencies, using a TEA5767 radio module, an Arduino Nano, with adjustable scanning speed via a potentiometer.

There are several examples that use Arduino out there, and I decided to use one, but I wanted to modify it to display the station numbers as it sweeps through them using a 4 digit 7 segment display, specifically a TM1637 display. That’s where I am having issues, I don’t know the proper way to code that into the project.

Edit: The specific display I am using has no decimal indicators, so it will display “1053” for 105.3. Thanks for pointing that out guix!

I have read up on the display, have tried different things, but I seem to be missing something. I took out all my attempts to output the numbers to the display and left only what I have in there to initiate the display.

This is what I have so far, and note that the radio sweeping code is not mine, I simply edited the frequencies to drop local stations from the scanning process, and that part works fine, though I wish there was a less “long winded” way to do it. That’s well out of my skill level at the moment.


#include <TM1637Display.h>
#include <Wire.h>

const int CLK = 9;                                         // set the CLK pin connection to the display
const int DIO = 8;                                        // set the DIO pin connection to the display

TM1637Display display(CLK, DIO); // set up the 4-Digit Display.

unsigned char frequencyH = 0;
unsigned char frequencyL = 0;
unsigned int frequencyB;
double frequency = 0;

int potpin = A0;  // analog pin used to connect the potentiometer
int val;    // variable to read the value from the analog pin

void setup()
{
  Wire.begin();
  frequency = 76.0; //starting frequency
  setFrequency();
  Serial.begin(9600);
  display.setBrightness(5);              // set the display to maximum brightness
  display.clear();
  
}

void loop()
{

   val = analogRead(potpin);            // reads the value of the potentiometer (value between 0 and 1023)
    delay(150);   
  frequency = 76.0;
  setFrequency();
  Serial.println(frequency);
    frequency = 76.1;
  setFrequency();
  Serial.println(frequency);
    frequency = 76.2;
  setFrequency();
  Serial.println(frequency);
    frequency = 76.3;
  setFrequency();
  Serial.println(frequency);
    

*snip* 
*You get the idea, this section of code repeats all the way up to 108MHz in .1 increments.*
  

}

void setFrequency()
{
  
  frequencyB = 4 * (frequency * 1000000 + 225000) / 32768;
  frequencyH = frequencyB >> 8;
  frequencyL = frequencyB & 0XFF;
  Wire.beginTransmission(0x60);
  Wire.write(frequencyH);
  Wire.write(frequencyL);
  Wire.write(0xB0);
  Wire.write(0x10);
  Wire.write((byte)0x00);
  Wire.endTransmission();
    val = analogRead(potpin);            // reads the value of the potentiometer (value between 0 and 1023)
 
  delay(val);
 
}

Thank you VERY much in advance. Any help getting this to output the frequency numbers to the display as it sweeps would be greatly appreciated!

Welcome,

Does your TM1637 module have dots to show for example “105.3”, or do you want to show “1053” ?

No, this specific display does not have the decimal, so it will just display “1053” for 105.3 and so on.

Sorry, I forgot to add that. I can edit that in. :slight_smile:

It should be as simple as doing display.showNumberDec( frequency * 10 );

1 Like

I was SO close!

I had display.showNumberDec( frequency);

If you don’t mind me asking, what does the *10 do?

Looks like I need to add it to each section of frequency code? Working so far. Now I just need to tackle the interference the display is causing with the radio. :stuck_out_tongue:

Thank you very much for your help!

* 10 is multiply by 10, so float 105.3 becomes int 1053

You should rewrite your setFrequency function by adding a parameter to it. Then instead of

frequency = 76.0;
setFrequency();
Serial.println(frequency);

You would do setFrequency( 76.0 );

void setFrequency( float freq )
{
  Serial.println( freq );
  display.showNumberDec( freq * 10 );
  
  ...
1 Like

In retrospect, that makes perfect sense. I will get the hang of this eventually.

As far as editing the setFrequency function, that looks SO much cleaner and easier!

I can’t thank you enough!

Edit:

Everything is working great. Solved the interference by moving the power for the display off the 5V line (resisted to 3V) to the Arduino’s 3.3V output directly.

Now I can move it all off the breadboard and onto a PCB. :wink:

IMG_20210427_121207297

Here is the current working code, in case anyone is interested.

#include <TM1637Display.h>
#include <Wire.h>

const int CLK = 9;                                         // set the CLK pin connection to the display
const int DIO = 8;                                        // set the DIO pin connection to the display

TM1637Display display(CLK, DIO); // set up the 4-Digit Display.

unsigned char frequencyH = 0;
unsigned char frequencyL = 0;
unsigned int frequencyB;
double frequency = 0;

int potpin = A0;  // analog pin used to connect the potentiometer
int val;    // variable to read the value from the analog pin

void setup()
{
  Wire.begin();
  frequency = 76.0; //starting frequency  setFrequency();
  Serial.begin(9600);
  display.setBrightness(5);              // set the display to maximum brightness
  display.clear();
  
}

void loop()
{
   val = analogRead(potpin);            // reads the value of the potentiometer (value between 0 and 1023)
    delay(150);
setFrequency( 76.0 );
setFrequency( 76.1 );
setFrequency( 76.2 );

//Add as many setFrequency( X ) lines as needed for your area

}

void setFrequency( float frequency )
{
  
  Serial.println( frequency );
  display.showNumberDec( frequency * 10 );
  frequencyB = 4 * (frequency * 1000000 + 225000) / 32768;
  frequencyH = frequencyB >> 8;
  frequencyL = frequencyB & 0XFF;
  Wire.beginTransmission(0x60);
  Wire.write(frequencyH);
  Wire.write(frequencyL);
  Wire.write(0xB0);
  Wire.write(0x10);
  Wire.write((byte)0x00);
  Wire.endTransmission();
    val = analogRead(potpin);            // reads the value of the potentiometer (value between 0 and 1023)
 
  delay(val);
 
}
1 Like

Some improvements to do :slight_smile:

  • in fact, you don’t need frequency as a float, if you multiplied by 100000 instead of 1000000
  • if you want to go from 760 to 1080, it’s a job for a for loop
  • use local variables whenever possible

Try this:

#include <TM1637Display.h>
#include <Wire.h>

const uint8_t potpin = A0; // analog pin used to connect the potentiometer
const uint8_t CLK = 9;     // set the CLK pin connection to the display
const uint8_t DIO = 8;     // set the DIO pin connection to the display

TM1637Display display(CLK, DIO); // set up the 4-Digit Display.

void setup()
{
  Wire.begin();
  Serial.begin(9600);
  display.setBrightness(5); // set the display to maximum brightness
  display.clear();
}

void loop()
{
  for ( uint16_t frequency = 760; frequency <= 1080; frequency++ )
  {
    setFrequency( frequency );
  }
}

void setFrequency( const uint16_t frequency )
{
  Serial.println( frequency );
  display.showNumberDec( frequency );
  const uint16_t frequencyB = 4 * (frequency * 100000 + 225000) / 32768;
  const uint8_t  frequencyH = frequencyB >> 8;
  const uint8_t  frequencyL = frequencyB & 0XFF;
  Wire.beginTransmission(0x60);
  Wire.write(frequencyH);
  Wire.write(frequencyL);
  Wire.write(0xB0);
  Wire.write(0x10);
  Wire.write((byte)0x00);
  Wire.endTransmission();
  delay(analogRead(potpin));
}
1 Like

I knew there had to be a way to get it to count through the range rather than individual lines. The only reason I didn’t look into it was the fact I had to omit several stations from the range. I want it to skip the local stations and only cycle through the dead ones. So I figured I would have to do it individually.

I do like the “full sweep” option, and it will help me learn, so I thank you for that!

Also, thanks for all the advice, it’s been very helpful!

I really need to stop changing this project, the more I think about it, the more I want it to do. Now I’m wondering if there’s a way to use a selector to switch between a full sweep and omitted sweep option. If not in code than I know I could do it in the hardware by simply adding a second Nano to the unit.

I don’t know when to quit. :smile:

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.