32u4 Basic Proto

8 MHz ATmega32u4 with 32K of flash and 2K of RAM.   The ATmega32u4 is well supported in the Arduino community.   20 GPIO pins including 7x PWM pins and 10x analog inputs.   The Arduino documentation for Leonardo/Micro boards applies to the Feather 32U4.  

Adafruit product page

Adafruit tutorial

Hardware pinout reference

WARNING: When using a pin for PWM, don't reference as "A7", "A9", "A10",...   Use "const byte pinLED = 6;" not "const byte pinLED = A7".  

Serial Communcations

Serial1   Pins 0 & 1 are hardware serial port "Serial1" and uses the native USB capabilities of the ATmega32u4.  

Software serial using other pins require that they support "change interrupts".   In order to use a second non-USB UART, use pin #10 for Rx, and #11 for Tx.  


setup() {
  Serial.begin(9600);	// USB
  Serial1.begin(9600)	// pins 0 & 1
}

void() {
	if (Serial1.avaiable()) {
		Serial.println(Serial1.read());
	}
}

 


/*
  Adafruit Feather 32u4 Basic Proto

  ATmega32u4 chip running at 8 MHz
  Built-in LED on pin #13
  Battery voltage measurement on pin A9 (#9)
  Hardware UART is Serial1 on pins #0 and #1
 
 */

// In Arduino IDE, select 'Tools','Board','Adafruit Boards','Adafruit Feather 32u4'
// Find USB port in Windows by looking at ...

/////////////////////////////////////////////////////////////////////////
const byte pinBuiltInLED = 13;  
byte stateBuiltInLED = LOW;
/////////////////////////////////////////////////////////////////////////
// blinkLEDnoDelay()
unsigned long LEDblinkPeriod = 8;
unsigned long LEDblinkLast = 0;
byte LEDblinkPWM = 0;
bool LEDblinkState = false;
byte LEDlastMode = 0;

void blinkLEDnoDelay(byte pin, byte mode) {
  // Blink the LED on 'pin' without using delay() according to
  // the 'mode' argument defined below. 
  // pin must support PWM. 
  // 
  // mode:
  //  0 = breathing
  //  1 = blink slow constantly
  //  2 = blink fast constantly
  //  3 = slow burst every 1 second
  //  4 = fast burst every 1 second
  //
  // Required global variables: LEDblinkPeriod, LEDblinkLast, LEDblinkPWM, LEDblinkState, LEDlastMode
  if (mode == 0) {
    // breathing
    LEDblinkPeriod = 8;
    if (LEDlastMode != mode) {
      LEDblinkPWM = 0;
      LEDblinkState = true;
      digitalWrite(pin, LOW);
    }
    if (millis() - LEDblinkLast >= LEDblinkPeriod) {
        if (LEDblinkPWM > 254) LEDblinkState = false;
        if (LEDblinkPWM < 1) LEDblinkState = true;
        if (LEDblinkState) {
            LEDblinkPWM++;
        } else {
            LEDblinkPWM--;
        }
        analogWrite(pin, LEDblinkPWM);
        LEDlastMode = mode;
        LEDblinkLast = millis();
    }
  } else if (mode == 1) {
    // blink slow constantly
    LEDblinkPeriod = 1000;
    if (millis() - LEDblinkLast >= LEDblinkPeriod) {
        digitalWrite(pin, LEDblinkState);
        LEDblinkState = !LEDblinkState;
        LEDlastMode = mode;
        LEDblinkLast = millis();
    }
  } else if (mode == 2) {
    // blink fast constantly
    LEDblinkPeriod = 100;
    if (millis() - LEDblinkLast >= LEDblinkPeriod) {
        digitalWrite(pin, LEDblinkState);
        LEDblinkState = !LEDblinkState;
        LEDlastMode = mode;
        LEDblinkLast = millis();
    }
  } else if (mode == 3) {
    // slow burst every 1 second
    // Slow 4 blinks (lazy burst) followed by 1 sec pause
    if (LEDlastMode != mode) {
      LEDblinkPWM = 0;
      LEDblinkState = true;
      LEDblinkPeriod = 100;
    }
    if (millis() - LEDblinkLast >= LEDblinkPeriod) {
        if (LEDblinkPWM < 7) {
          if (LEDblinkPWM == 0) LEDblinkState = true;
          digitalWrite(pin, LEDblinkState);
          LEDblinkPeriod = 100;
          LEDblinkState = !LEDblinkState;
          LEDblinkPWM++;
        } else {
          digitalWrite(pin, LOW);
          LEDblinkPWM = 0;
          LEDblinkPeriod = 1000;
        }
        LEDlastMode = mode;
        LEDblinkLast = millis();
    }
  } else if (mode == 4) {
    // fast burst every 1 second
    // Fast 4 blinks (burst) followed by 1 sec pause
    if (LEDlastMode != mode) {
      LEDblinkPWM = 0;
      LEDblinkState = true;
      LEDblinkPeriod = 25;
    }
    if (millis() - LEDblinkLast >= LEDblinkPeriod) {
        if (LEDblinkPWM < 7) {
          if (LEDblinkPWM == 0) LEDblinkState = true;
          digitalWrite(pin, LEDblinkState);
          LEDblinkPeriod = 25;
          LEDblinkState = !LEDblinkState;
          LEDblinkPWM++;
        } else {
          digitalWrite(pin, LOW);
          LEDblinkPWM = 0;
          LEDblinkPeriod = 1000;
        }
        LEDlastMode = mode;
        LEDblinkLast = millis();
    }
  } // mode
}   // blinkLEDnoDelay()


void blinkLED(byte ledPIN){
  //  consumes 300 ms.
  for(int i = 5; i>0; i--){
    digitalWrite(ledPIN, HIGH);
    delay(30);
    digitalWrite(ledPIN, LOW);
    delay(30);
  }    
} //blinkLED()


void blinkERR(byte ledPIN){
  // S-O-S
  const int S = 150, O = 300;
  for(int i = 3; i>0; i--){
    digitalWrite(ledPIN, HIGH);
    delay(S);
    digitalWrite(ledPIN, LOW);
    delay(S);
  }    
  delay(200);
  for(int i = 3; i>0; i--){
    digitalWrite(ledPIN, HIGH);
    delay(O);
    digitalWrite(ledPIN, LOW);
    delay(O);
  }    
  delay(200);
  for(int i = 3; i>0; i--){
    digitalWrite(ledPIN, HIGH);
    delay(S);
    digitalWrite(ledPIN, LOW);
    delay(S);
  }    
  delay(200);
} // blinkERR()

//////////////////////////////////////////////////////////////////////////////
float fReadBatt() {
  // Voltage float at 4.2V with no battery connected.
  float f = analogRead(A9);
  f *= 2;     // voltage divider is in battery circuit
  f *= 3.3;   // reference voltage
  f /= 1024;  // convert to voltage
  return f;
} // freadBatt;

//////////////////////////////////////////////////////////////////////////////
// TimerA
// 10000000 us = 10000 ms = 10 sec = 0.1 Hz 
// 1000000 us = 1000 ms = 1 sec = 1 Hz
// 100000 us = 100 ms = 0.1 sec = 10 Hz
// 10000 us = 10 ms = 0.01 sec = 100 Hz
// 1000 us = 1 ms = 0.001 sec = 1 kHz
const unsigned long timerIntervalA = 2000;  
unsigned long timerAlap = millis();  // timer

void timerA() {
  //  Timer A
  if (timerAlap > millis())  timerAlap = millis();
  if (millis() - timerAlap > timerIntervalA) { 
    // do something here every timerIntervalA / 1000 sec
    //blinkLED(pinBuiltInLED);
    
    Serial.print("Battery: ");
    Serial.print(fReadBatt());
    Serial.println("V");
    
    // Serial1 test
    Serial1.println(millis());

    if (stateBuiltInLED == HIGH) {
      stateBuiltInLED = LOW;
    } else {
      stateBuiltInLED = HIGH;
    }
    digitalWrite(pinBuiltInLED, stateBuiltInLED);

    timerAlap = millis(); // reset the timer
  }
} // timerA()

/////////////////////////////////////////////////////////////////////////


void setup() {

  pinMode(pinBuiltInLED, OUTPUT);
  digitalWrite(pinBuiltInLED, LOW);

  Serial.begin(115200);
  while (!Serial) {
    delay(100);
    if (millis() % 2)
      digitalWrite(pinBuiltInLED, HIGH);
    else
      digitalWrite(pinBuiltInLED, LOW);
  }
  Serial.println("\nSerial ready");

  Serial1.begin(9600);
  while (!Serial1) {
    delay(100);
    if (millis() % 2)
      digitalWrite(pinBuiltInLED, HIGH);
    else
      digitalWrite(pinBuiltInLED, LOW);
  }
  Serial.println("Serial1 ready");

  Serial.println("setup finished\n");
} // setup()


void loop() {

  //blinkLEDnoDelay(pinBuiltInLED, 4);
  timerA();

  // Install a jumper wire between DIO #0 & #1
  if (Serial1.available()) {
    digitalWrite(pinBuiltInLED, HIGH);
    char c = Serial1.read();
    //Serial.write(c);
    Serial.print(c);
  }
  
} // loop()

 


Do you need help developing or customizing a IoT product for your needs?   Send me an email requesting a free one hour phone / web share consultation.  

 

The information presented on this website is for the author's use only.   Use of this information by anyone other than the author is offered as guidelines and non-professional advice only.   No liability is assumed by the author or this web site.