Program Resource

Resource libraries for programmers and developers

There’s QR code library for Arduino released 3 years ago.

https://github.com/ricmoo/qrcode/

Well made library, works with less memory of Arduino while supporting QR code versions and error correction levels.

You can easily create QR code data with above library and show code to OLED display with u8glib library.

Below is sample sketch for showing QR code with “https://programresource.net” text data to OLED display, with inverted B/W on left / right half of screen. Very short and simple sketch which does amazing work.

#include "qrcode.h" //https://github.com/ricmoo/qrcode/
#include "U8glib.h"

U8GLIB_SSD1306_128X64 u8g(U8G_I2C_OPT_NONE | U8G_I2C_OPT_DEV_0);   // I2C / TWI

void display_qrcode(char *text) {	
	QRCode qrcode;
	uint8_t qrcodeData[qrcode_getBufferSize(4)]; //Version 4 114 byte 33x33 res QR code
	qrcode_initText(&qrcode, qrcodeData, 4, ECC_LOW, text);

	u8g.firstPage();
	do {
		u8g.setColorIndex(0);
		u8g.drawBox(0, 0, 64, 64);
		u8g.setColorIndex(1);
		for (uint8_t y = 0; y < qrcode.size; y++) {
			for (uint8_t x = 0; x < qrcode.size; x++) {
				if (qrcode_getModule(&qrcode, x, y))
					u8g.drawBox(15 + x, 15 + y, 1, 1);
			}
		}
		
		u8g.setColorIndex(1);
		u8g.drawBox(64, 0, 64, 64);
		u8g.setColorIndex(0);
		for (uint8_t y = 0; y < qrcode.size; y++) {
			for (uint8_t x = 0; x < qrcode.size; x++) {
				if (qrcode_getModule(&qrcode, x, y))
					u8g.drawBox(79 + x, 15 + y, 1, 1);
			}
		}
	} while ( u8g.nextPage() );
}

void setup() {
	u8g.begin();
	display_qrcode("https://programresource.net");
}

void loop() {
}
Print Friendly, PDF & Email

This post is also available in: Japanese

Leave a Reply

Your email address will not be published. Required fields are marked *


*

CAPTCHA