N2CMU Arduino
N2CMU Arduino Library

Arduino CI Arduino Lint Spellcheck CI Arduino Release License: GNU GPL v3.0

This is the official Arduino library for N2CMU (Neural Network Coprocessing Microcontroller Unit).

Arduino N2CMU Shield

Getting Started

To start using n2cmu-arduino library in your Arduino projects, follow these simple steps:

  1. Open your Library Manager on Arduino IDE.
  2. Type n2cmu-arduino and click "Install."

Alternatively, you can follow the steps below:

  1. Download the n2cmu-arduino library from the GitHub repository.
  2. Extract the downloaded archive and rename the folder to n2cmu-arduino.
  3. Move the n2cmu-arduino folder to the Arduino libraries directory on your computer.
    • Windows: Documents\Arduino\libraries\
    • MacOS: ~/Documents/Arduino/libraries/
    • Linux: ~/Arduino/libraries/
  4. Launch the Arduino IDE.

Example Usage

Below is an example usage of N2CMU Arduino library that demonstrates an example training for NAND gate training and inference.

#include <n2cmu.h>
void setup() {
// Initialize serial communication
Serial.begin(9600);
while(!Serial);
// Initialize the N2Coprocessor instance
N2Coprocessor coprocessor;
if(coprocessor.begin())
Serial.println(F("Co-processor initialized!"));
else {
Serial.println(F("Something went wrong. Halting..."));
while(true);
}
// Reset the CPU
if(coprocessor.cpuReset())
Serial.println(F("CPU Reset!"));
else {
Serial.println(F("Something went wrong. Halting..."));
while(true);
}
// Initialize neural network with 2 input, 2 hidden, and 1 output neurons
Serial.println(F("Initializing neural network..."));
coprocessor.createNetwork(2, 2, 1);
coprocessor.setEpochCount(4000);
// Define training dataset and corresponding output
float dataset[][2] = {{0, 0}, {0, 1}, {1, 0}, {1, 1}};
float output[][1] = {{1}, {1}, {1}, {0}};
// Start network training
Serial.println(F("Starting network training..."));
if(coprocessor.train((float*) dataset, (float*) output, 4, 1.0f))
Serial.println(F("Training done!"));
else {
Serial.println(F("Something went wrong. Halting..."));
while(true);
}
// Perform inferences
Serial.println(F("Attempting inferences..."));
for(uint8_t i = 0; i < 4; i++) {
float output[1];
if(coprocessor.infer(dataset[i], output)) {
Serial.print(F("\t["));
Serial.print(dataset[i][0]);
Serial.print(F(", "));
Serial.print(dataset[i][1]);
Serial.print(F("]: "));
Serial.println(output[0]);
}
else Serial.println(F("Inference attempt failed."));
}
// Reset the network
Serial.println(F("Inference done, resetting network."));
coprocessor.resetNetwork();
}
void loop() {
delay(1000);
}
Class representing the N2CMU device.
Definition: n2cmu.h:47
bool infer(float *input, float *output)
Make inference with the neural network using provided input data.
Definition: n2cmu.cpp:146
void createNetwork(uint8_t inputCount, uint8_t hiddenCount, uint8_t outputCount)
Create a neural network with specified input, hidden, and output neuron counts.
Definition: n2cmu.cpp:104
bool train(float *data, float *output, uint16_t len, float learningRate)
Train the neural network with provided data and output.
Definition: n2cmu.cpp:119
bool cpuReset()
Reset the CPU of the N2CMU device.
Definition: n2cmu.cpp:97
bool begin()
Initialize the N2CMU device.
Definition: n2cmu.cpp:86
void setEpochCount(uint16_t epoch)
Set the epoch count for training.
Definition: n2cmu.cpp:206
void resetNetwork()
Reset the neural network parameters.
Definition: n2cmu.cpp:160
Header file for N2CMU (Neural Network Coprocessing Microcontroller Unit).

PCB Schematic Diagram

Arduino N2CMU Shield Schematic Diagram

License

Copyright (c) 2024 Nathanne Isip

n2cmu-arduino is distributed under the GNU General Public License v3.0. For further details, refer to the LICENSE file.

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, version 3.
This program is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.