The Basics of Using Pins

At the heart of every application is input and output. With microMighty, there’s all kinds of input and output that can be used and this lesson explains the basics of using the different types of input and output (IO) available.

In this tutorial You'll

  • Write to the on board LEDs and IO pins
  • Open a General Purpose IO pin as Input and Ouput
  • Read the General purpose digital IO pins
  • Group pins to make a multi-bit vector
  • Write to a multi-pin vector
  • Apply timing parameters to IO functions


In this lesson, we’re going to be using the microMighty to control some output. So, the first thing to do is to attach microMighty to the USB port of your computer and open your serial terminal connection. The terminal will connect with microMighty and display a prompt that is waiting for some code. (The REPL)

Part 1: General purpose digital Output

IO in microMighty can take many forms: digital, analog, pulsed, serial, and even more specialized things like neoPixel. Right now though we’re going to concentrate on digital IO. To use almost any IO in microMighty, we’re going to use the “pin” module to control the pins of the board, that is the connections that microMighty has to the outside world.

At the prompt, type:

pin.write(, 1 )

What happened?  The BLUE led lights up. What happens when we change the “1” to a “0” in the code above, so now our line of code looks like:

pin.write(, 0 )

As you probably expected, the LED now turns off.

microMighty has 4 LEDs (Blue, Green, Red, and Yellow). If you haven’t already figured out what to use to control the other LEDs, here’s a little table with all the other values used to access the other LEDs:

Value LED

So, now that you know what all of the LEDs are called, try changing the pin.write() call to turn on a different LED.

  • What do you think will happen  when you type: pin.set( )

  • What do you think will happen when you type: pin.clear( )

  • What do you think will happen when you type: pin.toggle( )

Part 2: General purpose digital Input

Now that you know how to use a pin to output a digital value, let’s try to read a value from a digital input. At power up, each pin of the microMighty is unconfigured (except for the LEDs), so, it's good practice to configure the pins before we use them.  Type the following line into the microMighty terminal to configure the pin we’re going to use as an input: 0, pin.mode.INPUT )

Viola! The pin is now configured to be an input and we can now use it to read the state of the pin. So? How do you read a pin?  Try typing: 0 )

Lua will return the state of the pin as 1 (high -- 3.3V) or 0 (low -- GND).  Now, connect a jumper wire from the D0 pin to the GND connection (it’s right next to D0) and type 0 ). What happens?  microMighy returns 0. Try connecting the jumper to the 3.3v pin (that's one away from D0) just be careful, we don’t want to accidentally connect the 3.3v and GND!

And now, Typing: 0 )

Will return a value of 1!  But, what if we want to access more than one pin?

Part 3: More than one pin

The Lua extensions of  microMighty have been specially designed to make working with peripherals (stuff like sensors, lights, displays, and motors) a whole lot easier to understand.  And, working with the stuff that’s connected to microMighty might require more than one pin. So we could use a bunch of pin write statements, or we can take advantage of a feature of microMighty… vectors!  Very simply, a vector is a group of pins that we want to access all at the same time. These pins are stored in a Lua table most significant bit first. So, defining a pin vector like:

pinVector = { 7, 2, 4, 10 }

Would mean that a value of 8 written to the vector would set pin D7 and clear D2, D4, and D10. Using the LEDs, we can experiment a little. What do you think will happen after entering the following code snippet:

for led = 0, 16 do
   pin.write( {pin.yellow,,,}, led)
   cpu.delay( 500 )

As you probably guessed, the blue LED blinks at a half second rate, the green at a 1 second rate, the red at a 2 second rate and the yellow at a 4 second rate. Or, said another way, the LEDs count up to a count of 16 in binary.

Part 4: Generating Pulses (blinking an LED)

Using what we’ve already learned, do you think that you could blink one of the LEDs? Well, we could use a bunch of pin.write() calls to turn the LED on and off, or maybe use the pin.set() pin.clear() combo. Possibly, we could use the pin.toggle() call to change the state of the LED.

That doesn’t seem too hard. But, microMighty doesn’t restrict the pins that you use in a vector. What do you think would happen if we made a vector that had the same pin defined more than one time?!?

vector = {,}

What would happen if we used pin.write() to write a value of 1 to vector? You might have guessed that the blue LED turns on. But what if we wrote a 2?  The blue LED turns off?!? This is because when microMighty needs to write different values to the same pin, it defaults to MSB first and inserts a small delay of about a microsecond between writes. Yep, both values are written. And since a value of 2 has the MSB set and the LSB clear the end result is the LED being turned off.

So entering:

pin.write( vector, 2 )

Is the same as:

pin.write ( vector, 2, 1 )

But what is that 3rd parameter?!?  Try changing the 1 to something real big like 250000. Our code will now look like:

pin.write( vector, 2, 250000 )

What happens? The LED turns on then off. That 3rd parameter is the time between writes. You can use the optional 3rd parameter for most pin module functions to control the rate at which the pins are written… or read!  It is specified in microseconds so, a value of 250000 is 250 milliseconds or a quarter second. This can be a handy way to do things like generate pulses on pins to start an operation, or for example, generate a pulse to start an ultrasonic module distance measurement.