Ross Satchell
Published © GPL3+

How to use the Arduino Import Tool in MPLAB X

This guide shows you how to import an Arduino project into MPLAB X, so that you can set breakpoints, debug, use Watches, and I/O View

IntermediateProtip30 minutes73
How to use the Arduino Import Tool in MPLAB X

Things used in this project

Hardware components

Microchip AVR-DB Curiosity Nano dev board
×1

Software apps and online services

Arduino IDE
Arduino IDE
MPLAB X IDE
Microchip MPLAB X IDE

Story

Read more

Code

Arduino Project

Arduino
This project uses the potentiometer, OLED display, and NeoPixel ring on the new Microchip Curiosity Nano Explorer board
// Include libraries
#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <Adafruit_NeoPixel.h>

// OLED display parameters
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels
#define OLED_RESET -1    // Reset pin # (or -1 if sharing Arduino reset pin)
#define SCREEN_ADDRESS 0x3D // See datasheet for Address; 0x3D for 128x64, 0x3C for 128x32

// NeoPixel parameters
#define PIN PIN_PE3
#define NUMPIXELS 9
#define DELAYVAL 500

// Define LED pins

#define LED0 PIN_PB0  //LED0
#define LED1 PIN_PB1  //LED1
#define LED2 PIN_PC0  //LED2
#define LED3 PIN_PC1  //LED3
#define LED4 PIN_PA0  //LED4
#define LED5 PIN_PA1  //LED5
#define LED6 PIN_PB2  //LED6
#define LED7 PIN_PB3  //LED7
#define POT PIN_PD6   //POT

// Initialize NeoPixels
Adafruit_NeoPixel pixels(NUMPIXELS, PIN, NEO_GRB + NEO_KHZ800);

// Initialize OLED display
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);

uint32_t result = 0;
const int POT_MIN = 0;
const int POT_MAX = 4095;
const int SEGMENT_SIZE = POT_MAX / (NUMPIXELS); // Divide the range into segments for each NeoPixel

void setup() {
  Serial3.begin(9600);

  if (!display.begin(SSD1306_SWITCHCAPVCC, SCREEN_ADDRESS)) {
    Serial3.println(F("SSD1306 allocation failed"));
    for (;;); // Don't proceed, loop forever
  }

  ADC0.CTRLA = 0x1;
  pixels.begin(); // Initialize NeoPixel strip
}

void loop() {

  uint16_t result = analogRead(POT);

  int segment = result / SEGMENT_SIZE; // Calculate which segment the potentiometer reading falls into
  pixels.clear(); // Clear all NeoPixels

  for (int i = 0; i < segment && i < NUMPIXELS; i++) {
    if(i % 2 == 0) { // Check if the index is even or odd
      pixels.setPixelColor(i, pixels.Color(10, 0, 0)); // Set red color for even indices
    } else {
      pixels.setPixelColor(i, pixels.Color(0, 0, 10)); // Set blue color for odd indices
    }
  }
  pixels.show(); // Update NeoPixels

  // Display potentiometer value on OLED
  display.setTextSize(2);
  display.setTextColor(SSD1306_WHITE);
  display.setCursor(0, 0);
  display.clearDisplay();
  display.print("Pot: ");
  display.println(result); // Print potentiometer value
  display.println(); // Empty line


  // Calculate and display potentiometer percentage
  double percentage = (double)result / POT_MAX * 100;
  display.print("Pot%: ");
  display.print(percentage, 1); // Print potentiometer percentage with one decimal place
  display.println();

  display.display();
}

Credits

Ross Satchell

Ross Satchell

5 projects • 5 followers
Marketing Applications Engineer with 8-bit MCUs. Started with Arduino and progressed to PICs and AVRs. Develop embedded systems for fun too!

Comments