DIY Function Generator in 3 Lines of Code

A simple DIY function generator that you can make using only 3 lines of code

Similar projects worth following
Make your own programmable Function Generator using only 3 lines of Python code!

Function generator comes really handy when you are testing your circuits, a professional one will cause you a fortune, but you can make by your own to save a lot.

The most commonly used waveforms are Square wave, Sine wave and Triangular wave. In this project, we will create these 3 different waveforms, change their frequency and duty cycle on the fly.

Function generator is a very common testing tool in a electronic lab and they may be used as a signal source to test analog circuit or to introduce an error signal into a control loop to verify circuit's performance.

To DIY a function generator, there are 3 challenges,

1. Creating a repetitive signal

2. Configurable frequency of the signal

3.  Configurable Waveforms

To overcome challenge 1 and 2, we can use a microcontroller with a PWM signal which is a repetitive square wave with controllable frequency. However, to do it in 3 lines of code, we must rely on the very convenient and powerful MicroPython project. It allows a microcontroller to interpret Python code and change its behaviors on the fly, which is ideal for our case. Here we are using Ameba AMB23 (RTL8722DM) with MicroPython to generate the PWM signal.

Moreover, to overcome challenge 3, we need a little help from a simple circuit, and a bit of math. As demonstrated from below, a square signal actually consist of many high frequncy components.

If we are able to remove those high frequency components, we will be able to create triangualr, sawtooth or even sine waves. Therefore, we need to design some simple Low Pass Filters to pass only the Low frequency components of a signal, here is the design,

As you can see in the picture, there are 4 sets of low pass filter, each consists of a 1K resistor and a 0.1 capacitor, and we can easily draw different waveforms from their output.

The 3 lines of code is shown in video above and in the picture down below,

  • 1 × Ameba AMB23 RTL8722DM
  • 1 × Breadboard Electronic Components / Misc. Electronic Components
  • 3 × jumper wire
  • 4 × 1K Resistor
  • 4 × 0.1 Capacitor

  • 1
    Download MicroPython firmware into your AMB23

    Follow the "Getting Started Guide" to download and flash the pre-built firmware into your Microcontroller

  • 2
    Build the circuit with resistors and capacitors

    follow schematic found above to build the circuit

View all instructions

Enjoy this project?



Similar Projects

Does this project spark your interest?

Become a member to follow this project and never miss any updates