Building your own Function Generator – Display

Leave a comment

I am glad to report that I was able to progress from idea to prototype stage. I was able to stitch the individual modules together to build a working solution. Bear in mind that this is no way a professional setup, but, it will be a good tool for enthusiasts. This is going to be a long post, but, stick with me and perhaps you could learn something interesting from this. Ok. Let us dive in.

The first piece of the puzzle was to display the selected frequency on screen. As I detailed earlier I have some 4 digit 7 segment LED modules on hand and I prefer to use them for the display needs. When I bought the LED module, it  was a very odd for the reason that there was no good datasheet and so I had to fiddle with my Bus Pirate and try to display something on it. It was very frustrating at that time. But, I was able to unearth a datasheet and was able to finally understand the LED module’s expectations and Bus Pirate’s usage with help from Ian of Dangerous Prototypes. I always tell Ian that he has a great smile. Watch some of his videos and you will know what I mean. Ian is very down to earth and friendly. If you are hearing about Bus Pirate for the first time, then, please visit Dangerous Prototypes to learn more about this wonderful debugging tool. Coming back to the LED module, here is the forum link where I documented the Bus Pirate commands. You can also see a grainy picture of my Bus Pirate and the taste of success of displaying a 0 on screen.  All these happened late last year.

Around the same time, Aaron, another forum user raised an issue of chaining multiple displays together and I put together a Shift register based dual LED displays that could be controlled via Energia. You could read more about this from this forum link. I have included the Energia code and a video of displaying numbers on 2 LED displays as part of the forum link. Even though I just show 2 LED displays, the idea could be extended to 8 such displays using a single shift register. Energia is an Arduino type IDE for Texas Instruments Launchpad for MSP430 series. Energia is code compatible with Arduino and so it has some merits. First it was only $4.30 including shipping when they originally released and so I jumped on that offer. It is still available through distribution channels and through TI’s e-store for $9.99 at the time of this writing. So it is very cheap compared to our Arduino. You can take a look at the board layout and Pin mappings here. TI actually included the development board, male and female edge connectors, crystal, additional controller, USB cables and some cool stickers nicely boxed up and delivered through FedEx. So it was a great buy if you ask me.

So I knew how these LED displays operate and was planing to write some wrapper functions to make our job here easier. Accidentally I stumbled upon the work by gblanton. Gblanton has already done the heavy lifting for us in terms of writing a useful class library for Arduino for the exact same display. I tried it out and found that it worked well and so I do not have to reinvent the wheel here. I just adopted his work for our Function Generator’s display.

The AD9850 signal generator module could go up to 40 MHz and it can generate the waveform down to 1 Hz. This made me initially decide that I could have a way to switch the selection from Hz to kHz to MHz. But, on second thoughts I added 2 displays so as to get all 8 digits (40, 000, 000). Now we can precisely control every single digit from Hz to MHz instead of rounding the frequency to fit the 4 digit display. Gblanton has some sample code as part of his website and so I see no reason to include my code here. If you find his work useful, then, please leave a note of thanks.

Now the 2 displays are sitting side by side and they display 10, 000, 000 (10 MHz) as the initial value.

Time to move to the next step. Using the rotary encoder to change the value of a selected digit and then, use the push button as part of the encoder to switch between different digits. Stand by, more to come on the usage of Rotary Encoders in the next post.

Building your own Function Generator

Leave a comment

Function generators are nice to have as part of one’s test equipment collection. But, the conventional desktop function generators are in the order of few hundred dollars and is mostly out of reach for an average enthusiast. But, with the availability of DDS chips and modules, a simple signal generator could be easily built. I have one of the DDS modules based on Analog Devices AD9850 and I was able to procure that for under $4. This module cannot function on its own, and needs commands from a micro-controller. So Arduino can come in handy. I have seen quite a few example codes on the web to program Arduino to send in a control command for a specific frequency and then use that for testing. I however want to build this as a small standalone test equipment.

The idea is to have function generator with it’s own LED 7 segment display to read out the frequency setting and a small cheap rotary encoder to adjust the frequency. Of course Arduino will be the brains behind getting user inputs through the rotary encoder and provide necessary feedback by displaying the frequency selection on the LED 7 segment display. And, finally talk to our DDS module to command it to generate the appropriate frequency for display.

I have in my possession a SPI compatible 4 digit 7 segment LED display from Oasis. The display in itself is very cheap, it comes to a little over a $1 each. Since most of the heavy lifting will be taken care by the PT6961 Controller itself, we can concentrate of interfacing Ardunio to the DDS module and not get side tracked by rendering contents on the display and refreshing the display periodically. It still leaves us to handle the inputs from our cheap rotary encoder. Rotary encoders are all over ebay for under $2 each. I plan to first bread board the solution using Arduino and then downsize it to just a single chip.

So, some fun time ahead. We need to learn to do the following:

  1. Display frequency selection on 7 segment LED display
  2. Interface with rotary encoder and get user input for frequency selection
  3. Interface with the push switch of the rotary encoder and get Hz, kHz and MHz selection
  4. Interface with DDS module to send command signals to generate the appropriate frequency
  5. Debounce the signals from the rotary encoder (my cheap encoder will definitely need this step)

More to come on this subject after some experimentation.

Arduino wants to meet new friends

Leave a comment

Now that we know how to to make Arduino talk to us through serial communication channel TX/RX, our next step is to find a way to say something back to Arduino and build some intelligence so that Arduino can understand us. When I look back to the good old days of BBC MicroCommodore 64 and ZX Spectrum class of home computers, I have very fond memories of writing simple programs in Basic, but, I had a lot of fun doing so. It was an era when people wanted to be friends with their beloved computer. I figured why not do the same with Arduino. After all it is much more powerful compared to my ZX Spectrum.

So here is what we want to do. Arduino will talk to us and we will reply and that in turn will be understood by Arduino and we have a conversation. Think that you are just 8 to 10 years old and your only objective is to learn and interact with your new found friend Arduino.

Do not get taken aback by the long code posting. When we get to the bottom of this, you will understand the critical parts and the silly imaginations of the 10 year old. But, you will have one happy Arduino with a friendly personality. So let is first dive into the code.

// Arduino interacts with the user

// when we type something back to Arduino the text we type
// should be stored in a temporary buffer before we get a
// chance to use it
String userName = "";
// even thought Arduino cannot think by itself, it is very fast
// when we type the response back, we are relatively slow
// after all we humans cannot communicate in 1s and 0s
// this variable is here to tell Arduino when we have finished typing
boolean userEntryComplete = false;

// we want to initialize serial communications
void setup()
  // the buffer through which we communicate back to Arduino
  // needs to be allocated

  // we will setup Arduino to communicate with the PC
  // at 9600 baud (9600 bits per second)

  // we will let Arduino introduce itself first
  Serial.println("Greetings! My name is Arduino.");
  // let Ardunino ask us our name next
  Serial.println("May I know with whom I am talking to?");
  // asking for the name more than once will be weird
  // so we are asking as part of the setup block and not inside the loop

void loop()
  // we do not want Arduino to process anything until we finished typing
  // so we will let Arduino to check to see whether we have done before
  // it starts to process
    if(userName != "")
      // user entered something
      // if the entry is yes or no, then Arduino needs to behave intelligently
      // think that these are commands that Arduino needs to process
      if (userName.equalsIgnoreCase("no"))
        // Arduino needs to just wait to meet someone new
        // you guessed it, Arduino is sad
        Serial.println(":( ok. Bye for now.");
      else if (userName.equalsIgnoreCase("yes"))
        // this is great news for our Arduino, it gets to meet with someone new
        // Arduino is happy and eager to meet with a new friend
        Serial.println(":) Please introduce me to your friend. What is your friend's name?");
        // time for Arduino to say Hello and find out whether there is a a posibility
        // of meeting another new friend
        Serial.println("Hello! " + userName + ". Welcome to the world of Arduino!");
        Serial.println("Nice to know you " + userName + ". Do you have a friend with you?");
      // May be our user does not understand English or perhaps out right mean
      // But, Arduino will be polite and will remind the user that it is expecting
      // the user to enter his/her name
      Serial.println("Hello! I am Arduino. Please enter your name.");
    // we need to initialize these parameters so that Arduino will wait for
    // further user interaction
    userEntryComplete = false;
    userName = "";

  // we are done, but, Ardunio does not know that
  // poor Arduino will loop through this block forever

// function is automatically called when serial input data is available
// but this availability of serial data is checked only after
// each processing of our loop code
void serialEvent()
  // we will try to read the serial input only when one or more
  // bytes of data is available for us to read
  // Serial.available() method returns the number of bytes available
  while (Serial.available() > 0)
    // we can use the read() method to read the value
    // but, read() only returns the value of a byte (0 to 255)
    // we already know that user is typing through the keyboard
    // and the data will be in ASCII characters and so we will
    // convert the value we receive to a character
    char oneCharacter = (char);
    // unless we receive the special New Line character from serial monitor
    // as a signal for us to stop, we will add/concatenate the characters together
    // to form a name; most people have more than one alphabet unless we meet with
    // Q from Star Trek: Enterprise
    if (oneCharacter == '\n')
      userEntryComplete = true;
      userName += oneCharacter;

That was long, but, if you exclude the comments and silliness there is not much to it. I have been liberal with the comments so that it helps the reader to understand why the next code block is written and what it is expected to do.

Let us fire up the above program and see how Arduino behaves. I suggest that you follow this write up to understand how it works and perhaps experiment with your own variant of the above code (by typing it and not copy and paste the code).

After you compile and upload the code to your Arduino, open up Serial Monitor and you should see the following.


Pay attention to the baud rate and Newline section. It is important that these selections are made correctly, as our code above assumes these settings.

That’s good. Arduino is waiting for us to type in our name. You can type in your name and press the send button.


Cool! It works. I have another friend next to me and so I will type yes and press the send button.



I have Pooni next to me and so I will introduce her to Arduino.


Unfortunately we have run out of company and so I will type no and press send.


Bye Arduino!

Will Arduino slowly learn to become Mr.Data?

Why not?

Older Entries