nuget-drivers-librerias-nanoframework

Drivers and NuGet Libraries in nanoFramework

  • 4 min

A NuGet package in nanoFramework is a ready-to-use library for adding drivers and functionality to your project without writing everything from scratch.

Up to this point in this protocols block, we’ve been true “byte artisans”. We’ve sent manual commands over I2C, calculated checksums, and configured registers bit by bit.

It’s great for learning how things work under the hood. But, being honest, if you had to write the complete driver for an OLED display or a 6-axis accelerometer from scratch every time you start a project, you would never finish anything.

In the .NET ecosystem, this kind of help has a well-known name: NuGet.

In this post, we’ll learn how to search, install, and use driver libraries already created by the community, saving us hundreds of hours of work.

What is NuGet?

If you come from Arduino, you know the “library manager”. If you come from Node.js, you know npm. In the .NET world, the king is NuGet.

It’s a centralized repository where developers upload compiled code packages (.dll) ready to use.

nanoFramework has an official organization and a very active community that ports and maintains drivers for almost any popular sensor you can buy on AliExpress or Amazon.

The nanoFramework.IoT.Device Repository

The nanoFramework.IoT.Device repository brings together implementations for nanoFramework of numerous drivers from the .NET IoT ecosystem.

This means we have access to tested and robust drivers for:

  • Sensors: BMP280, BME680, AHT10/20, DHT11/22, MPU6050…
  • Displays: SSD1306 (OLED), ILI9341 (TFT), Hitachi HD44780 (LCD 16x2)…
  • Expanders: PCF8574, MCP23008…
  • Motors: Stepper drivers, Servos (PCA9685)…

You can see the full list of supported devices in the nanoFramework.IoT.Device GitHub repository. The list is huge!

How to Search and Install a Driver

Let’s go through the process step by step in Visual Studio 2022. Suppose we want to use a BMP280 pressure and temperature sensor.

Open the Package Manager

In the Solution Explorer, right-click on your project (not on the solution) and select Manage NuGet Packages….

Search for the Device

Go to the Browse tab.

Here’s the trick: In the search box, type nanoFramework.IoT.Device followed by the chip name. For example: nanoFramework.IoT.Device.Bmxx80.

(Note: Sometimes drivers group families. The BMP280 is in the Bmxx80 family alongside the BMP180).

Include Prerelease Versions When Necessary

Many nanoFramework libraries are constantly updated. If the driver you’re looking for doesn’t appear, make sure to check the “Include prerelease” checkbox next to the search bar.

Install the Package

Select the package and click Install. Visual Studio will show you a confirmation window and will likely tell you it will install other dependencies (like System.Device.I2c). Accept everything.

From Byte to Object

To show you the difference, let’s compare how you would read the temperature “by hand” vs. “with a driver”.

Manual Register Access

(Simplified, without error handling or actual calibration)

// Configure I2C
var device = I2cDevice.Create(new I2cConnectionSettings(1, 0x76));

// Request reading (0xFA is the temp register on BMP280)
device.Write(new byte[] { 0xFA });
byte[] data = new byte[3];
device.Read(data);

// Combine bytes, apply compensation formulas (A mathematical nightmare!)...
// int adc_T = (data[0] << 12) | (data[1] << 4) | (data[2] >> 4);
// double temp = ... (complex formulas from the datasheet)
Copied!

Access Using a NuGet Driver

using Iot.Device.Bmxx80;
using Iot.Device.Bmxx80.FilteringMode;

// 1. Instantiate the object passing the I2C device
// Notice the driver already knows which registers to touch
var i2cSettings = new I2cConnectionSettings(1, Bmp280.DefaultI2cAddress);
var i2cDevice = I2cDevice.Create(i2cSettings);

using Bmp280 bmp = new Bmp280(i2cDevice);

// 2. High-level configuration (IntelliSense helps us)
bmp.TemperatureSampling = Sampling.HighResolution;
bmp.FilterMode = Bmp280FilteringMode.X2;

// 3. Read
var reading = bmp.Read();

Debug.WriteLine($"Temperature: {reading.Temperature.DegreesCelsius} ºC");
Debug.WriteLine($"Pressure: {reading.Pressure.Hectopascals} hPa");
Copied!

Not only is there less code. The key is that we get objects with units. It doesn’t return an int 2500; it returns a Temperature object we can request in Celsius, Fahrenheit, or Kelvin.

Automatic Dependencies

Another great advantage of NuGet is that it manages the dependency tree.

If you install a driver for a display that works over SPI (like nanoFramework.IoT.Device.Ssd1351), NuGet will automatically detect that your project needs the base library nanoFramework.System.Device.Spi and install it for you.

This avoids the classic “Missing reference to…” error.

What If the Driver Doesn’t Exist?

It happens sometimes. You have a very new or very rare sensor. In that case:

  1. Check if the driver exists in .NET IoT and see what APIs it uses. Not all drivers can be ported without changes, because nanoFramework implements a subset of .NET.
  2. Consult the datasheet or a reference library and port the logic to C# using operations like WriteRead.

With NuGet drivers, we can focus on the application without repeating the register access logic for each sensor. The next block takes that data off the device using Wi-Fi and networks.