Using the GPIO Pins of the HelvePic32

Getting started - the switch

Let's start with the switch and demonstrate how to read a digital pin. For this, we plug the ringCoder module into a breadboard and connect the following pins:

  • GND - Ground
  • VCC - 5V(+5V)
  • SW - pin 3 on the right side (nP[RIGHT][3])

The ringCoder breakout has a built in pull-down resistor for the switch pin.


The following picture illustrates the setup:

To read the state of the pins, we just need to make a call to

digitalRead(nP[RIGHT][3])

The same rules apply as with the Arduino, so you can use the common code on debouncing the input.to use the switch as you like.


 

Writing to a Pin

Now that we have seen how to read a pin state, we can also change the pin state by writing to it. This is similar to the Arduino using

digitalWrite(nP[RIGHT][3], HIGH);
digitalWrite(nP[RIGHT][3], LOW);

To see the effect of these calls, we connect the ringCoder pins used for the RGB Led to the HelvePic32:

Writing a HIGH to one of the pins will light up that color. Setting all pins to high like this:

digitalWrite(nP[RIGHT][0], HIGH);
digitalWrite(nP[RIGHT][1], HIGH);
digitalWrite(nP[RIGHT][2], HIGH);

will show a white light.

 

However, this is not what we expect from a RGB Led. As the HelvePic32 is capable to use PWM on each pin, we can change the intensity of each pin from 0 to 255. The code for this is shown here:

 

#include <HelvePic32.h>
#include <SoftPWMServo.h>

int rpin = nP[RIGHT][2];
int gpin = nP[RIGHT][1];
int bpin = nP[RIGHT][0];
int rval;
int gval;
int bval;

void setup(){
        pinMode(rpin, OUTPUT);
        pinMode(gpin, OUTPUT);
        pinMode(bpin, OUTPUT);
}

void loop(){
        for (int i=0; i<256; i++){
                Wheel(i);
                SoftPWMServoPWMWrite(rpin, rval);
                SoftPWMServoPWMWrite(gpin, gval);
                SoftPWMServoPWMWrite(bpin, bval);
                delay(30);
        }
}

void Wheel(byte WheelPos) {
        if(WheelPos < 85) {
                rval = WheelPos * 3;
                gval = 255 - WheelPos * 3;
                bval = 0;
        } 
        else if(WheelPos < 170) {
                WheelPos -= 85;
                rval = 255 - WheelPos * 3;
                gval = 0;
                bval = WheelPos * 3;
        } 
        else {
                WheelPos -= 170;
                rval = 0;
                gval = WheelPos * 3;
                bval = 255 - WheelPos * 3;
        }
}

The function Wheel() is used to nicely step through the colors of the rainbow.

 

The key difference to the Arduino Code is the call to SoftPWMServoPWMWrite(). It has the same syntax as analogWrite() from the Arduino with the differnece, that I can use it with any pin!