Autonomous Robot Project - Make a Test Aide - Square Wave Generator

About: Have enjoyed a mostly fun ride in an electronics and software career. Also enjoy latin dancing.

Intro: Autonomous Robot Project - Make a Test Aide - Square Wave Generator

The motivation for this Instructable is the longer one being developed, that tracks the progress through Texas Instruments Robotics System Learning Kit Lab Course. And the motivation for that course is to build (re-build) a better, more robust robot. Also helpful is "Section 9: Voltage, Power, and Energy Storage in a Capacitor, DC Engineering Circuit Analysis", available at MathTutorDvd.com.

When building a robot (not a toy) and noise (EMF, EMI) is a concern, we may wish to test some of our circuits for frequency-response (how they handle the noise)

We can buy a function/signal generator, or we can make our own very simply. Making a very simple one may do for our needs. We can use any micro-controller. (Think : Arduino, others). I'm not refering to Raspberry because typically, you will not get a steady, consistent pulse. However using a Raspberry may still be useful if you're not concerned about the jitter.

(If you only want the micro-controller board to do your own thing (I am composing a series of Instructables that might be helpful), the MSP432 development board itself is relatively inexpensive at around $27 USD. You can check with Amazon, Digikey, Newark, Element14, or Mouser.)

Step 1: The Software

I'm sure there's already code out there for the Arduino, but here's some psuedo-code that might be a learning / fun exercise.

But before we get into the real psuedo-code, let's demo the XOR ability of C / C++.

You can try this:

#include <stdio.h>
int main(void) {
        int someVal = 0;
        for (int i=0;i<10;i++) {
                someVal ^= 0x01;
                printf("%d\n",someVal);
        }
        return 0;
}

You can't get too much simpler than that. The above code assumes you want to toggle only one bit (bit 0, the least significant bit). It's all because of the "^" (caret).

Of course I used a straight C program and environment, but you should be able to do the similar in yours.

Or if you don't like that one, try this one:

#include <stdio.h>
int main(void) {
        int someVal = 0;
        for (int i=0;i<10;i++) {
                someVal = 0x01==someVal?0x00:0x01;
                printf("%d\n",someVal);
        }
        return 0;
}

That one uses the tertiary command (condition true)? (yes, assign this) : (no, assign this instead) ;

Or finally, this one:

#include <stdio.h>
int main(void) {
        int someVal = 0;
        for (int i=0;i<10;i++) {
                if (0x01==someVal) {

                        someVal = 0x00;
                } else {

                        someVal = 0x01;
                }
                printf("%d\n",someVal);
        }
        return 0;
}

Ok, here's the psuedo-code for the square-wave generator:

// square-wave signal generator
// psuedo code for any micro-controller (Arduino?)

// you'll need 2 pushbutton switches. (SW1, SW2)
// you'll need 3 ports/pins on the micro controller.
// the switches are to increase / decrease the frequency.

// connect one end of SW1 to input pin,
// connect other end to V+ (3.3V ? 5V ?)
// repeat same for SW2.

// you might want to get one or two LEDs, that's
// another one or two port/pins.
// the LEDs are to indicate if a switch was pressed.
//////////////////////////////////////////////////////////



1. figure out which port/pin to use for SW1.
2. set it to input, low.

3. repeat for SW2.

4. figure out which port/pin to use for square wave output.
5. set it to output , low.


6. declare someFrequencyVariable and set it to some starting
frequency. (NOT zero)

//START of MAIN LOOP

	////////////////////////////////////////////////
	//toggle pin. you can do an 'if() else ' statment,
	// or XOR - see the C code examples above.
	////////////////////////////////////////////////

	////////////////////////////////////////////////
	// you need a delay here. this is a key part.
	// if you have some high level functions available,
	// it may just be some delay function and you pass
	// the number of milli / micro seconds.
	// BUt - you may have to instead be aware of
	// how fast your micro-controller's clock is running.
	//
	// in any case, freqency =  1 / time.
	//
	// frequency is cycles per second
	//
	// BUT - since every time through this loop it just
	// toggle HI or LO but not both (You could do that, too
	// but it changes this process a bit...)
	// then you would need 1/2 of the delay.
	//
	// the idea is you want 1/2 cycle HI, then 1/2 cycle LO.
	//
	// if you end up having to divide by the freq
	// val make sure it can't be zero.
	////////////////////////////////////////////////


	////////////////////////////////////////////////
	declare somePermButtonVariable and make it zero.
	////////////////////////////////////////////////


	////////////////////////////////////////////////
	// here you figure out if there was a button pushed
	// it would be cool if the button read does NOT
	// block so that you can light an LED as long
	// as button is being held pushed.
	// 
	// you'll need to have two variables to capture this.
	// one is the immediate one of button pushed.
	// the other one holds that same info , but keeps it
	// even after no more button is pushed.
	////////////////////////////////////////////////
	while ( (buttonPushed = someButtonNonBlockingReadFunction()) != 0) {
		// code steps in here as long as button
		// is being held pushed (non-zero)

		/////////////////////////////////
		// light LED
		/////////////////////////////////

		/////////////////////////////////
		// assign button val to
		// the more perm variable
		/////////////////////////////////
		somePermButtonVariable = buttonPushed

	} // end of while loop


	/////////////////////////////////
	// if we got to here, the button
	// is no longer being pushed.
	/////////////////////////////////

	/////////////////////////////////
	// was there a buttonPushed?
	// is 'somePermButtonVariable non zero?
	//
	// if it IS zero , then no button
	// pushed, jump back to start of loop
	// dont waste anymore time
	// keep toggling (pulsing)
	/////////////////////////////////

	/////////////////////////////////
	//  if we got to here,
	// a button was pushed.
	//
	// turn OFF LED(s).
	/////////////////////////////////

	/////////////////////////////////
	// a button WAS pushed,
	// figure out which one,
	// and either increase or decrease
	// the current frequency Variable.
	//
	// if it is to decrease,
	// make sure you can't go below
	// a positive value.
	/////////////////////////////////

//END of MAIN LOOP


Step 2: An Example Application

Share

    Recommendations

    • Metalworking Contest

      Metalworking Contest
    • Audio Contest 2018

      Audio Contest 2018
    • Tiny Home Contest

      Tiny Home Contest

    Discussions