Arduino Web Server – Part 2


In my previous post, I showed you how to use AJAX and JavaScript to make a very responsive Web server on Arduino. In Part 2, we will look at making some important modifications.

Arduino has limited storage space – Use an SD Card

As all of us already know, the Arduino, especially the Uno and Nano, has very limited storage space. If we want to create a truly useful IoT Web server device, we need to do something to increase the available storage space on our Arduino Device.

We can however not increase the program memory. What we can do is store our static HTML page, as well as any images and icons, on a SD-CARD. These come in many sizes,
but for our example, I will use a 2gb card, not that we will use all of it anyway.

The standard Arduino Ethernet Shield also comes standard with a SD-CARD reader slot already built in.

Arduino Ethernet Shield for Arduino Uno or Mega. Note That SD Card Slot is already built in

This makes our lives a lot easier. You can also buy a stand-alone SPI SD Card module for a few bucks online. This will be needed if you try to make this project using an Arduino Nano.

Preparing the Card for use

You should format your card using your computer, and a suitable adapter. The card should be formatted to the FAT filesystem. NTFS or other filesystems does unfortunately not work as far as I know.

SD Card IO – How to use the SD Card in Arduino

The Arduino IDE already includes an SD Card library. You can also download additional libraries from the internet that allows more specialized control and functionality. The standard library will however be sufficient for our needs.

It is also easy to test if your card is working or not. The code below is from the “CardInfo” example that ships with the Arduino IDE

  SD card test

  This example shows how use the utility libraries on which the'
  SD library is based in order to get info about your SD card.
  Very useful for testing a card when you're not sure whether its working or not.

  The circuit:
    SD card attached to SPI bus as follows:
 ** MOSI - pin 11 on Arduino Uno/Duemilanove/Diecimila
 ** MISO - pin 12 on Arduino Uno/Duemilanove/Diecimila
 ** CLK - pin 13 on Arduino Uno/Duemilanove/Diecimila
 ** CS - depends on your SD card shield or module.
 		Pin 4 used here for consistency with other Arduino examples

  created  28 Mar 2011
  by Limor Fried
  modified 9 Apr 2012
  by Tom Igoe
// include the SD library:
#include <SPI.h>
#include <SD.h>

// set up variables using the SD utility library functions:
Sd2Card card;
SdVolume volume;
SdFile root;

// change this to match your SD shield or module;
// Arduino Ethernet shield: pin 4
// Adafruit SD shields and modules: pin 10
// Sparkfun SD shield: pin 8
const int chipSelect = 4;

void setup() {
  // Open serial communications and wait for port to open:
  while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB port only

  Serial.print("\nInitializing SD card...");

  // we'll use the initialization code from the utility libraries
  // since we're just testing if the card is working!
  if (!card.init(SPI_HALF_SPEED, chipSelect)) {
    Serial.println("initialization failed. Things to check:");
    Serial.println("* is a card inserted?");
    Serial.println("* is your wiring correct?");
    Serial.println("* did you change the chipSelect pin to match your shield or module?");
    while (1);
  } else {
    Serial.println("Wiring is correct and a card is present.");

  // print the type of card
  Serial.print("Card type:         ");
  switch (card.type()) {
    case SD_CARD_TYPE_SD1:
    case SD_CARD_TYPE_SD2:

  // Now we will try to open the 'volume'/'partition' - it should be FAT16 or FAT32
  if (!volume.init(card)) {
    Serial.println("Could not find FAT16/FAT32 partition.\nMake sure you've formatted the card");
    while (1);

  Serial.print("Clusters:          ");
  Serial.print("Blocks x Cluster:  ");

  Serial.print("Total Blocks:      ");
  Serial.println(volume.blocksPerCluster() * volume.clusterCount());

  // print the type and size of the first FAT-type volume
  uint32_t volumesize;
  Serial.print("Volume type is:    FAT");
  Serial.println(volume.fatType(), DEC);

  volumesize = volume.blocksPerCluster();    // clusters are collections of blocks
  volumesize *= volume.clusterCount();       // we'll have a lot of clusters
  volumesize /= 2;                           // SD card blocks are always 512 bytes (2 blocks are 1KB)
  Serial.print("Volume size (Kb):  ");
  Serial.print("Volume size (Mb):  ");
  volumesize /= 1024;
  Serial.print("Volume size (Gb):  ");
  Serial.println((float)volumesize / 1024.0);

  Serial.println("\nFiles found on the card (name, date and size in bytes): ");

  // list all files in the card with date and size | LS_DATE | LS_SIZE);

void loop(void) {

Open this example in your Arduino IDE, and then make sure that the CS pin is set to the correct pin for your Ethernet Shield ( Pin 10 is for Ethernet, Pin 4 is usually for the SD Card).

Both of these devices will be connected to the SPI bus on your Arduino, and the CS pin will determine which device is active, by being pulled LOW.

Insert your formatted card into the slot, power on the Arduino, and upload the sketch to the Arduino. Open the Serial monitor. If all goes well, you should see information about your SD Card ( Size, sectors etc being displayed ). If your card was already formatted to the FAT file system and contained other files, the names of these files will also be displayed.

Create your Web Page

Power down the Arduino, and remove the SD Card. Put it into the relevant adapter and connect it to your computer.

Now, open a plain text editor, notepad on windows, or any other specialized html editor, as long as you feel comfortable with it, and create a simple html file. Feel free to use my example below, and modify it to your liking

<!DOCTYPE html>
        <title>Arduino SD Card Web Page EXAMPLE - Maker and IOT Ideas</title>
        <h1>Welcome to your Arduino Based Web Server</h1>
        <p>This page is stored on the SD Card connected to your Arduino.</p>
        <p>Please do not remove the card while the Arduino is connected to a power source</p>


Save this file as index.htm, and remove the card from your computer, making sure that you properly stop it as per the standard procedures for your operating system.

Put it back into the slot on the Arduino Ethernet Shield, open the serial monitor, and apply power to your Arduino. make sure that you see the file, index.htm listed in the output.

Coding your Webserver

Our next step will be to write the code to create our Arduino Web Server. This code will be similar to the code in part 1 of this series, but I recommend that you start fresh, open a new sketch, and copy-paste my code into the IDE. you can always modify it later to suit your needs…

#include <SPI.h>
#include <Ethernet.h>
#include <SD.h>

// MAC address from Ethernet shield sticker under board
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
IPAddress ip(192, 168, 100, 32); // IP address, may need to change depending on network
EthernetServer server(80);  // create a server at port 80

File webFile;

void setup()
    // initialize SD card
    Serial.println("Initializing SD card...");
    if (!SD.begin(4)) {
        Serial.println("ERROR - SD card initialization failed!");
        return;    // init failed
    Serial.println("SD card initialized. [OK]");
    // check for index.htm file
    if (!SD.exists("index.htm")) {
        Serial.println("ERROR - Can't find index.htm file!");
        return;  // can't find index file
    Serial.println("index.htm file found - Starting Webserver");

    Ethernet.begin(mac, ip);  // initialize Ethernet device
    server.begin();           // start to listen for clients
    Serial.begin(9600);       // for debugging

void loop()
    EthernetClient client = server.available();  // try to get client

    if (client) {  // got client?
        boolean currentLineIsBlank = true;
        while (client.connected()) {
            if (client.available()) {   // client data available to read
                char c =; // read 1 byte (character) from client
                // last line of client request is blank and ends with \n
                // respond to client only after last line received
                if (c == '\n' && currentLineIsBlank) {
                    // send a standard http response header
                    client.println("HTTP/1.1 200 OK");
                    client.println("Content-Type: text/html");
                    client.println("Connection: close");
                    // send web page
                    webFile ="index.htm");        // open web page file
                    if (webFile) {
                        while(webFile.available()) {
                            client.write(; // send web page to client
                // every line of text received from the client ends with \r\n
                if (c == '\n') {
                    // last character on line of received text
                    // starting new line with next character read
                    currentLineIsBlank = true;
                else if (c != '\r') {
                    // a text character was received from client
                    currentLineIsBlank = false;
            } // end if (client.available())
        } // end while (client.connected())
        delay(1);      // give the web browser time to receive the data
        client.stop(); // close the connection
    } // end if (client)

Upload the sketch to your Arduino and navigate to the IP Address of the server using your browser. You should see the page displayed as you coded it.

What to do from here

You can now modify your page to include links and even images and CSS styling. You should however remember that the Arduino also does not have a lot of RAM memory.
You should thus not add extremely large images or pages. Those will take a long time to display, or may even time-out and not display at all

In the next part of this series, I will show you how to add links, images and CSS to make your page look a bit more visually appealing. We will also integrate the AJAX and JavaScript
functionality from the previous part of the series, to allow our server to interact with the inputs and outputs on the Arduino.

Arduino Web Server using AJAX – Part 1

There are many ways to use Arduino to create your own IoT device. One of the easiest is to configure your Arduino as a Web Server. This way, you can connect to it from any browser capable device on your home network. It is also quite a bit safer to do it this way, as you don’t have to expose your device to the internet, reducing the security implications of many of the other methods available.

It does, however, have the disadvantage of not being able to connect to your device from outside your home network. ( In a later part of this series, I will show you how to do this relatively safely, but take note that you still won’t have SSH encryption to the device, that is a huge security risk in today’s online world.

What use will this kind of IoT device have?

You can use an Arduino based web server to monitor various devices in your home, as well as control them. Many of us have old Android tablets and other devices lying around, that may be to old to run the newest Android Operating System. Such a Tablet can however be mounted to a wall, to provide a permanent display and control device. You will only be limited by your imagination, as well as your skill with interfacing your devices with electronics and Arduino.

A few examples of this can be
– controlling lights
– controlling a fan
– measuring temperature and light levels using various sensors, and performing actions based on those values

But Arduino Web Servers are slow

The normal Arduino web servers that we have all seen in various projects on the internet are indeed slow and cumbersome. This is because they usually have to refresh and reload the entire page to display every single update of a switch or output. We can however take advantage of technology used on computer web servers, as well as the browser of the end user.

Ajax and Javascript

What is AJAX?

AJAX stands for Asynchronous JavaScript and XML.

AJAX is basically the use of JavaScript functions for getting information from the webserver (Your Arduino). This means that data on a web page can be updated without fetching the whole page each time.

This means that only the relevant part of the web page will be updated, either automatically, or when the end user performs an action, like click on a button, or when an input on the actual Arduino changes state.

What is JavaScript?

JavaScript is a client-side scripting language. This means that the code will run on the web browser. Meaning on the end-user computer or mobile device.

The JavaScript is included in the HTML page that will be served by the Arduino Webserver. When you load the web page hosted by the Arduino, the page and the JavaScript is loaded to your browser. Your browser then runs the JavaScript code (provided that you have not disabled JavaScript in your browser).

What will we need to do this?

You will need the following hardware and software to do this project

– Arduino UNO or Compatible
– Arduino Ethernet Shield with NO SD-Card Inserted
– Breadboard
– 1K resistor
– 10k OR 22k Resistor
– Hookup Wire (5 pieces)
– Push Button

The Circuit

Wire the following circuit on your breadboard

Circuit Diagram for the Arduino AJAX Web Server – Part 1

Connect your Ethernet Shield to the Arduino Uno.
Connect the Resistors, LED and push-button Switch as shown.
Connect +5v from Arduino to Red Line, Gnd From Arduino to Blue line

Connect Orange Wire from Arduino Pin 2 to a hole BETWEEN the 10k resistor and the switch ( See Diagram).
Connect Green Wire from Arduino Pin 3 to a hole above the 1k resistor ( See Diagram)

The Code

Copy the following code into your Arduino IDE, Or download the file below

#include <SPI.h>
#include <Ethernet.h>
// MAC address from Ethernet shield sticker under board
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
IPAddress ip(192, 168, 100, 32); // IP address, may need to change depending on network
EthernetServer server(80); // create a server at port 80
String HTTP_req; // stores the HTTP request

void setup()
Ethernet.begin(mac, ip); // initialize Ethernet device
server.begin(); // start to listen for clients
Serial.begin(9600); // for diagnostics
pinMode(2, INPUT_PULLUP); // switch is attached to Arduino pin 2
pinMode(3, OUTPUT);

void loop()
EthernetClient client = server.available(); // try to get client
if (client) { // got client? 
boolean currentLineIsBlank = true; 
while (client.connected()) 
{ if (client.available()) 
{ // client data available to read char c =; // read 1 byte (character) from client HTTP_req += c; // save the HTTP request 1 char at a time 
// last line of client request is blank and ends with \n 
// respond to client only after last line received 
if (c == '\n' && currentLineIsBlank) { // send a standard http response header client.println("HTTP/1.1 200 OK"); 
client.println("Content-Type: text/html"); 
client.println("Connection: keep-alive"); 
client.println(); // AJAX request for switch state 
if (HTTP_req.indexOf("ajax_switch") > -1) 
// read switch state and send appropriate paragraph text 
} else 
// HTTP request for web page // send web page - contains JavaScript with AJAX calls 
client.println("<!DOCTYPE html>"); 
client.println("<title>Arduino Web Page with AJAX - Maker and IoT Ideas</title>"); 
client.println("function GetSwitchState() {"); 
client.println("nocache = \"&nocache=\"\ + Math.random() * 1000000;"); 
client.println("var request = new XMLHttpRequest();"); 
client.println("request.onreadystatechange = function() {"); 
client.println("if (this.readyState == 4) {"); 
client.println("if (this.status == 200) {"); 
client.println("if (this.responseText != null) {"); client.println("document.getElementById(\"switch_txt\")\.innerHTML =this.responseText;");

client.println("\"GET\", \"ajax_switch\" + nocache, true);");
client.println("setTimeout('GetSwitchState()', 1000);");
client.println("Arduino Web Server with AJAX"); 
client.println("Switch Status on D2"); 
client.println( "Switch state: Not requested…"); 
// display received HTTP request on serial port
HTTP_req = ""; // finished with request, empty string
// every line of text received from the client ends with \r\n
if (c == '\n') {
// last character on line of received text
// starting new line with next character read
currentLineIsBlank = true;
else if (c != '\r') {
// a text character was received from client
currentLineIsBlank = false;
} // end if (client.available())
} // end while (client.connected())
delay(1); // give the web browser time to receive the data
client.stop(); // close the connection
} // end if (client)

// send the state of the switch to the web browser
void GetSwitchState(EthernetClient cl)
if (digitalRead(2)) {
cl.println("Switch at D2 is: OFF, LED at D3 is OFF");
else {
cl.println("Switch at D2 is: ON, LED at D2 is ON");

Upload the code to your Arduino

Testing the results

Open a web browser and go to the IP Address of the server ( the one you set in your code).
If you did everything correctly, you should see a screen similar to this.

The Ajax Web Server shows the button and LED is OFF

Now press the button

The web page should immediately update and tell you that the button is On, and the LED is ON

Ajax Web Server showing Status of Button and LED as ON

The Generated HTML will look like this

<!DOCTYPE html>



<title>Arduino Web Page with AJAX - Maker and IoT Ideas</title>


function GetSwitchState() {

nocache = "&nocache=" + Math.random() * 1000000;

var request = new XMLHttpRequest();

request.onreadystatechange = function() {

if (this.readyState == 4) {

if (this.status == 200) {

if (this.responseText != null) {

document.getElementById("switch_txt").innerHTML = this.responseText;

}}}}"GET", "ajax_switch" + nocache, true);


setTimeout('GetSwitchState()', 1000);




<body onload="GetSwitchState()">

<h1>Arduino Web Server with AJAX</h1>

<h3>Switch Status on D2</h3>

<p id="switch_txt">Switch state: Not requested...</p>



Images of the Working Hardware

Please note that I did not use a push button switch in my example. I have just used a piece of hookup wire to connect the pulled-up pin to ground, as it is easier to photograph that way, without my finger being in the way on a button. 🙂


This concludes part 1 of this series. This example can very easily be extended to be more useful, as well as be modified to work on other platforms, like ESP32.
In further parts, I will show you how to extend this very simple server into becoming something much more useful. Please visit again to see the rest of this series.

ESP32 (Kid Bright v 1.3) Voice-Activated IoT Relay Control using IFTTT and Adafruit IO

Today I will show you how to do a very quick IoT relay controller using IFTTT and Adafruit IO. I will be using the Kid Bright v 1.3 Development board, from Gravitec in Thailand. These boards sell for about $USD 25 to 35 each, quite expensive as far as I am concerned, for the amount of functionality that you get.

I will also post a link to the Video Tutorial at the bottom of this tutorial.

Kid Bright v 1,3 Development Board, Advanced user Diagram
Kid Bright 32 Schematic

You can find out more about this board on the Kid Bright Website. Please note that you will need Google Translate, as the site is in the Thai Language.

We will use IFTTT to connect Google Assistant to Adafruit IO in order to control our IoT Relay controller with voice commands.

Let us start our project.

Start the Arduino IDE, and make sure that you have enabled support for NodeMCU or other ESP32 based processors.
You also need to load the libraries for Adafruit IO.

After you have done that, open the “adafruitio_07_digital_out” sketch from the Examples folder for Adafruit IO. We will modify this example to suit our needs, as well as save some time on coding.

Change the Example to the following:
Remember to change the pins to reflect your particular setup.

include “config.h”

define LED_PIN 17
define Relay 27

// set up the ‘readinglight’ feed
AdafruitIO_Feed *readinglight = io.feed(“readinglight”);

void setup() {

pinMode(Relay, OUTPUT);

// start the serial connection

// wait for serial monitor to open
while(! Serial);

// connect to
Serial.print(“Connecting to Adafruit IO”);

// set up a message handler for the ‘readinglight’ feed.
// the handleMessage function (defined below)
// will be called whenever a message is
// received from adafruit io.
readinglight->onMessage(handleMessage); // Handler for our FEED

// wait for a connection
while(io.status() < AIO_CONNECTED) {

// we are connected


void loop() {

//; is required for all sketches.
// it should always be present at the top of your loop
// function. it keeps the client connected to
//, and processes any incoming data.;


// this function is called whenever an ‘readinglight’ feed message
// is received from Adafruit IO. it was attached to
// the ‘digital’ feed in the setup() function above.
void handleMessage(AdafruitIO_Data *data) {

Serial.print(“received <- “);

if(data->toPinLevel() == HIGH)

digitalWrite(LED_PIN, !data->toPinLevel()); // We inverse the logic on the LED, we want it on //when the relay is on, and off when the relay is off
digitalWrite(Relay, data->toPinLevel()); // My Relay Module is Active LOW, so this is correct.
//reverse the logic if your module is Active High

Another very important step is that you need to set your Adafruit IO IO-Key in the config.h file. Your Wifi SSID and Password also needs to be supplied here, to enable your ESP32 based processor to connect to Adafruit through your WiFi connection.


/ Adafruit IO Config */
define IO_USERNAME “your AdafruitIO username”
define IO_KEY “your AdafruitIO IO Key”
define WIFI_SSID “your wifi ssid”
define WIFI_PASS “your wifi password”

Leave everything else as is in the config.h file

Now login to Adafruit IO, or create a new account if you have not done so already.
Create a new feed, in my case called readinglight, under your feeds.

Click on the Adafruit IO Key button ( top right corner ) and copy your IO Key and Username into the config.h file in the Arduino IDE. Don’t upload your code yet.

Now go to IFTTT, and login or create a new account if you don’t have one already.
After you have logged in, in a new window, go to

Make sure to connect Adafruit and IFTTT, and allow IFTTT to send messages to Adafruit IO.

Then, follow the pictures, and create your first applet.

Select “Say a Simple phrase”
Configure it with what you want to say to activate the relay, Then Click “Create Trigger”
Click on :Adafruit”
Click Send Data to Adafruit IO
Select you feed from the drop-down box. IMPORTANT, USE A 1 to switch something ON, or a 0 to switch it OFF. HIGH AND LOW, or ON and OFF DOES NOT WORK ! Then Click on Create Action
Review your applet, Switch OFF notifications, and Click on Finish.

Now create another Applet, Doing exactly the same, but change your wording to “Light OFF” and the Adafruit IO Data to 0. This applet will be used to switch off the Relay.

Go back to the Feed window in Adafruit IO, and then test Google Assistant by saying your command, like “Hey Google, Light On” and “Hey Google, Light Off”

You should see data arriving in your feed window after each command. A 1 for On and a 0 for Off.

Now go back to your Arduino IDE, Open the Serial Monitor, and Compile and Upload your Code. Test it again, but now you should see data arriving in the Serial Monitor as well.

If all of this is working, You can connect your Relay to the board, and test it again.
When you are happy that all is working as it should, Connect a load to the relay, and enjoy your new Voice-Activated IoT Relay Controller.

It is also easy to add another feed to the existing code. Ask me how if you don’t understand, but it should be quite easy to figure out from the code as well 🙂
Voice Controlled IoT Relay controller using Google Assistant, IFTTT and Adafruit IO

A glance back in History – Manufacturing your own transistors – At home!

We are living in an industrial age, electronics components are easily available. They are dirt cheap. I would like to share a few pages from an old book that I have recently found on a book-archive site on the internet. It is freely available for download, so I thought I would add a bit of history and share it with my fellow electronics enthusiasts…

The Book is called “Practical Transistors and Transistor Circuits” by JS Kendal.
It was first published in August 1954…

This is chapter 1, which I find the most interesting, I sincerely hope you will find it interesting too.

I hope that you have enjoyed the journey back in time… Thank you

Extending Arduino/Esp32/STM32 GPIO Pins – PART 2

In the first part of this series, I showed you how to extend the available output pins on your microprocessor by using a SIPO (Serial In, Parallel Out) Shift Register. These work great to extend your outputs, but they do tend to involve a bit of extra work and organisation in your code. They are also a bit slower than the normal GPIO pins, because data has to be serially shifted into them, and then latched out onto the parallel port.

I have also mentioned that there are I2C devices available that can make this much easier… In today’s article, I will show you how to use one of these I2C devices, the PCF8574.

These little modules have some quite impressive features, for one, allowing you to cascade up to 8 of them together, giving you a quite impressive 64 GPIO ports ! I am also happy to tell you, that if you can find the PCF8574A variant, as well, you can increase the total amount of ports to 128! ( If you chain 8x PCF8574 as well as 8x PCF8574A together) This is possible because the I2C addresses of the two series of chips are different. Thus allowing us to add a total of 16 of them to the I2C bus.

It must however be said that you should calculate your bus resistance very carefully if you plan on doing that. For most of us, I do not believe we will need that much GPIO on a single microprocessor!

Enough introduction, let us start by looking closely at the chip, as well as the modules that you can purchase for around 1 USD each…

A word of caution, there is also another version of these available, which is specifically designed to be used with LCD screens. You should thus be careful when you buy a premade module, that you choose the io-extender version, and not the LCD controller version.

PCF8574 I2C IO Extender module – Front View
PCF8574 I2C IO Extender – Back View

As we can see, the GPIO ports are clearly labeled, from P0 to P7, with the INT (Interrupt Pin) on the very right.

As I have said before, you can cascade up to 8 of these onto the I2C bus. This is done by setting the I2C address of the module. This is done by setting the jumpers as seen in the picture below.

Address Jumpers on the PCF8574 I2C IO Extender Module

The Address can be set by using the following table to lookup the address and set the jumpers accordingly.

A2A1A0I2C Device Address
Available I2C Addresses for PCF8574 selected by setting the jumpers

Connecting the device is very easy. You only have to supply 5v and Ground, as well as connect it to the SCL and SDA Pins on your microprocessor. For Arduino Uno / Nano that is A4 (SDA) and A5 (SCL)

As far as the coding is concerned, you have two options. You can either use the built-in Wire library, or you can download a special library. Both works equally well, but I do believe that the built-in Wire library might be a little bit faster.

Another point to make is that there are a lot of “fake” modules on the market these days. These modules work, but some of them have extremely weak current sourcing abilities. ( I recently bought a pair online, and they are unable to properly light an LED even without a current limiting resistor. I fixed that issue by driving the LED through a small BJT transistor, like the 2n2222a.

You should also take note that the ports will start up in a weak HIGH state when the module is powered up. This should be taken into consideration when designing your circuit to drive external devices through the outputs. In other words, you should take precautions to prevent the devices from switching on before the microprocessor takes control of the module.

Let us start to look at the coding that you will need to do to use this device.
I will start with the built-in wire Library that is included with the Arduino IDE.

#include <Wire.h> // Wire.h provide access to I2C functions

void setup()
Wire.begin(); // Start I2C

void loop()

Wire.beginTransmission(0x20); // Our device is on Address 0x20
Wire.write(0x0F); // This is equal to 0b00001111, meaning it will switch ports P0 to P3 High
Wire.write(0xF0); // This is equal to 0b11110000, meaning it will switch ports P4 to P7 High

The code above will alternate between switching 4 ports high and low every one second.
You can observe this by connecting 8 LEDs through 330ohm resistors to ports P0 through P7

Reading the status of a port (meaning that you configured it as an input) can be done using the following code


void setup()
  Wire.write(0x00);  //LED1 is OFF

void loop()
  Wire.requestFrom(0x20, 4); // Read the state of P4
  byte x =;
  if (bitRead(x, 4) == LOW)
    Wire.write(0x01);  //LED1 is ON
    Wire.write(0x00);  //LED1 is OFF

This code assumes that you have connected a LED throught a resistor to P0, and that you have connected a pullup resistor of 10k to P4, with a pushbutton to GROUND.

The LED should switch on when you press the switch, and go off again once you release it.

If you want to use the special library, you can download it below:

Install this into your Arduino Libraries, and use the following code:

include “Arduino.h”

include “PCF8574.h

// Set i2c address
PCF8574 pcf8574(0x20);

void setup()

// Set pinMode to OUTPUT

pcf8574.pinMode(P0, OUTPUT);



void loop()
pcf8574.digitalWrite(P0, HIGH);
pcf8574.digitalWrite(P0, LOW);

Reading the status of an Input can be done like this:

include “Arduino.h”

include “PCF8574.h”

// Set i2c address
PCF8574 pcf8574(0x20);

void setup()

pcf8574.pinMode(P0, OUTPUT);
pcf8574.pinMode(P1, INPUT);


void loop()
uint8_t val = pcf8574.digitalRead(P1);
if (val==HIGH) Serial.println(“KEY PRESSED”);


There are also excellent examples included with the library. These include using the interrupt pin.

I hope that this will be useful to somebody.

Extending the Inputs and Outputs of your Arduino/ESP32/STM32

There will come a time that you will run out of available input or output pins on your Arduino/ESP32 or STM32 device. Today, I will show you one way to work around this problem and how to add additional input and/or outputs to your device.

There are many ways to do this, the easiest of them being adding some sort of i2c
chip (the Waveshare PCF8574 IO Expansion Board is a good example).

This particular device can be cascaded to provide up to 64 IO ports on the i2c bus.

At Maker and IoT Ideas, we would however like to show you electronics that you can build yourself, or stuff that have not already been made up into some kind of commercial project.

So, keeping this DIY attitude going, I will teach you how to use a shift register today.

Before we do that, we have to look at what a shift register is, and how they work…

A shift register, at its basic level, is actually just a series of D Type Flip-Flops.

D Type Flip Flop

These flip flops are connected together to give this

Parallel in, Serial out Shift Register

There are three basic types of Shift Register
– Serial In, Serial Out (SISO)
– Serial In, Parallel Out (SIPO) and
– Parallel In, Serial Out (PISO)

We can see that they these type of shift registers are mainly used to convert between an serial and parallel data interface.

Serial In, Serial Out ( SISO)

This is the easiest configuration for a shift register. It is basically just a row of flip flops, with the output of the first, connected to the input of the next… and so on. This type of shift register is mainly used to introduce a delay into the data stream. This means that for a 8 bit SISO Shift register, the first data will only appear at the output after 8 clock cycles !

Serial In, Parallel Out (SIPO)

This kind of Shift Register has the same configuration as the SISO type, but it differs in that there is output after every flip-flop. That makes this type of shift register a good choice to use to extend the outputs on a microcontroller like Arduino or ESP32. The downside of using a shift register as a parallel output device is that the outputs will be slightly slower than if you used the native outputs on the microcontroller.

Parallel In, Serial Out (PISO)

A Parallel In, Serial out shift register can be used to read inputs from buttons or other digital devices. These inputs are then serially shifted into a serial pin on the microcontroller to be processed. It is also slightly slower than if you used the native inputs on the microcontroller.

Universal Shift register

You are also able to use a universal shift register. These chips can be used as both inputs and outputs. They are however only available in a 4 bit configuration.

The Chips (IC’s)

SISO / SIPOTTL74HC5958 bit
SIPOTTL74LS1648 bit
PISOTTL74HC1668 bit
Some common Shift Register Chips

Example using Arduino with 74HC595n SIPO Shift Register

We will look at an example to connect the 74HC5959N to Arduino to drive eight (8) LEDs.
This example can be adapted to drive other loads as well by using a small BJT transistor and current limiting resistor instead of the LED

74hc595n Pin NumberDescription and Connection
Q0…Q7 (15,1,2,3,4,5,6,7)Parallel outputs of the shift register to write up to 8 signals with only 3 pins on your Arduino or ESP32 microcontroller
GND (8)Connect to the ground on the microcontroller
VCC (16)Connect to 3.3V or 5V on the microcontroller
DS (14)Serial data input has to be connected to the microcontroller (in this example D4)
OE (13)Output enable input do we not need and connected to ground
STCP (Latch) (12)Storage register clock input has to be connected to the microcontroller (in this example D5)
SHCP (11)Shift register clock input has to be connected to the microcontroller (in this example D6)
MR (10)Master reset connected to VCC because is active with LOW and we do not want to reset the register
Q7S (9)Serial data output not needed and therefore not connected
Connections for 74HC595n to Arduino

The operation of the 74hc595n SIPO Shift Register can easily be explained in three steps:

  1. The latch (STCP pin) is pulled LOW because the data is only stored in the register on a LOW to HIGH transition of this pin.
  2. Data is shifted out to the register with the data pin (DS) and with a LOW to HIGH transition of the clock signal (SHCP).
  3. The latch (STCP pin) is pulled HIGH to save the data in the register.

Make the following connections to your Arduino and Breadboard ( we will use Arduino Nano today, but you can use the same pins and code for Arduino Uno )

Breadboard Layout for 74HC595 Shift Register as Output Extender with Arduino Nano
Schematic Diagram for using 74HC595 Shift Register as Output Extender with Arduino Nano

Double-check all your connections, and then you can start the coding. The code will be much easier than you may think, as there is already a builtin function in the Arduino C language that we will use to shift the data out to the register.

// Define the Control Pins for the register
int latch-Pin = 5;
int clock-Pin = 7;
int data-Pin = 6;

void setup()
pinMode(latch-Pin, OUTPUT);
pinMode(data-Pin, OUTPUT);
pinMode(clock-Pin, OUTPUT);


void loop()
byte leds = 0;
for (int i = 0; i < 8; i++)
bitSet(leds, i);
for (int i = 7; i >= 0; i–)
bool b = bitRead(leds, i);
Serial.println(” “);

void updateShiftRegister()
digitalWrite(latch-Pin, LOW);
shiftOut(data-Pin, clock-Pin, LSBFIRST, less);
digitalWrite(latch-Pin, HIGH);

In this example, we output a HIGH to each led in turn, shifting from 0 to 7.

I hope that this will make sense to everybody, and be useful.
Questions and comments are welcome. Next time, we will look as using a PISO Shift register to extend the inputs on you Arduino/ESP32/STM32

Thank you.

Useful Circuits using NAND Gates – Logic Gates Part 2

In part one of this series, I showed you how to construct the basic logic gates using transistors and a few resistors. Today I will expand on that by showing you two very simple, but useful circuits, constructed with NAND gates, as well as a handful of other components.

The first circuit will be a Capacitive On/OFF Switch, Based on the CD4011 CMOS Quad NAND Gate. I draw the circuit to function on 5v, but you can also change the relay and use it with a supply voltage of up to 12v DC

Let use look at the circuit

Capacitive Switch using CD4011 CMOS Quad Nand IC

As we can see here, the two NAND gates are configured as a LATCH or R/S Flip Flop. Touching the “ON” touch plate causes a change in the input logic, making the latch change state and switching ON the output. Touching the “OFF” touch plate, resets the latch, switching the output OFF.

You can also send the input directly to a microcontroller like Arduino. In that case you would take the output at pin 1 through a resistor to the input of the microcontroller.

The circuit can also work in reverse logic, as pin 4 will be the complementary state of pin 1, thus off becomes 1 and on becomes 0

Our second circuit for today is a PWM motor controller, made using 4 NAND gates, a few resistors, capacitors, diodes, a mosfet and a variable resistor.

PWM Motor Controller using NAND Gates

In this circuit, Nand Gate U7.1 generates the PWM frequency, as well as changes the on-off period of the PWM signal via R37, as the user turns the pot, the charge-discharge time of C1 is changed, thus altering the duty cycle of the signal.

This is thus an effective, low component way to do PWM motor control without a microcontroller.

Next week, I will introduce another two usefull NAND Gate based circuits for you to try out.

Using MQTT with IoT Devices – A Short Summary

You are excited, you have just finished your new IoT device, and have many excellent ideas on how you will use it remotely, on your smartphone, or even from your computer at the office. You now start thinking about how you will send and receive data to this new device of yours…

There are many ways that you can do this, but today, I would like to suggest a very easy and lightweight data transfer protocol. It is called MQTT. Some of you may already be using it, or you may at least have heard about it. Read on if you would like to know more about what it is, as well as how it works.

The History of MQTT

MQTT was designed by Andy Stanford-Clark (IBM) and Arlen Nipper (Cirrus Link, then Eurotech) in 1999. It was first used to monitor an oil pipeline running through the desert. The goal was to design and implement a protocol that is bandwidth-efficient, lightweight and uses very little battery power, because the devices were connected via satellite link which, at that time, was extremely expensive.

In 2013, IBM submitted the MQTT v3.1 protocol to the OASIS specification body with a charter that ensured that only minor changes to the specification could be accepted.MQTT-SN is a variation of the main protocol aimed at embedded devices on non-TCP/IP networks, such as Zigbee.

Historically, the “MQ” in “MQTT” came from the IBM MQ (then ‘MQSeries’) MQ product line. The protocol, however, provides publish-and-subscribe messaging (no queues, in spite of the name) and was specifically designed for resource-constrained devices and low bandwidth, high latency networks. This makes it an excellent candidate for data transmission on IoT and other low resource devices.

The Protocol Architecture (How does it work)

MQTT uses a client-server architecture, where the server is called the broker, and the client, called a client. The broker typically functions like a post office, in the sense that it doesn’t use the client’s address, but rather the subject of the topic that a client is subscribed to, to determine which client should receive a certain message.

This enables many clients to subscribe to the same subject, with each receiving the same message. Clients can also publish a topic, thus transmitting or sending a message to other clients. This concept make bidirectional communication possible between clients, and it also ensures that it is extremely easy to use.

What do theses topics look like?

An MQTT Topic is a text string, delimited by a /
for example, let us say you have a device in your kitchen, that monitors the temperature, and controls the lights and microwave oven.

This device may use the following topics to publish (send data) or subscribe to (receive data)


As you can see, we can easily group topics by their location. It would thus be very easy for a smart home controller, like OpenHab or similar to get status or set a particular state in a certain room or area.

This can be done by using wildcard operators, of which there are two, the + and #

If we want to subscribe to all the lights in the house, we can subscribe to the following topic:


This will give us the data from the following topics:

You can also subscribe to a multilevel wildcard topic, for example:


This will subscribe you to all the topics related to the kitchen.

How do I get access to MQTT

There are quite a few online MQTT brokers available that allow you to apply for a free account. These services have many limits on the amounts of messages you may send or receive, but generally, they are quite useful. should be well known. It is stable, and easy to use. They do however have a few limitations on how many devices and messages you can add to the service. is a fairly unknown provider outside of Thailand. They limit you to 3 Projects, with 10 devices per project, but their messaging limits are extremely liberal and permissive. This service will also always be free (or at least that is according to NetPie themselves). On the negative side, all the configuration is available in English, but all the documentation is in Thai. You can however use Google Translate to translate it into a language that you can understand.

If you are not comfortable with running your data on an online broker, you can also download and install one of the many MQTT brokers that are available for installation on Linux and MS-Windows. This will, however, mean that you should also have permanent Internet access, as well as a public IP address, to enable your devices to connect to your broker.

Microprocessor and other Device support

MQTT Libraries are available for use on Arduino, STM32, ESP32 as well as Raspberry PI and Python. You can also get clients and brokers for the Linux and MS-Windows platforms.

Smartphone Access

Various applications exist on the google playstore, as well as the Apple App Store, that allow you to connect to an MQTT broker, subscribe to topics, and also publish topics. Many of these are free to use, and also provide you with a nice user interface.

You can also write your own smartphone app, if you are skilled enough, to do exactly what you want it do do.

Previous Posts

A short list of previous posts available on the old website

Arduino and STM32 “Blue Pill”

Arduino with LCD

Arduino with HC-SR04 Ultrasonic Sensor

Arduino nRFL01+ Rf Module Remote IO Extender

Arduino SPI data to STM32

How to program the STM32 “Blue Pill” with Arduino IDE

How to use the native USB port to upload code to the STM32 “Blue Pill”

Electronics tutorials and circuit diagrams

What is an H-Bridge Motor Controller

An H-Bridge Motor Controller Code Example for Arduino

PCB Files for building your own H-Bridge controller – FREE DOWNLOAD

Logic Building Blocks – The Logic Gates

Welcome to our New Site

Welcome to our new website on It has been a while, but I finally managed to get the domain and hosting issues sorted out. (It took some time as I am doing everything myself, as I believe that hiring a developer to do something as easy as a simple blog type site is a wasted opportunity to do something myself.)

I have become a bit rusted with these ICT skills, as I did not need to do these things for a few years now. This site will bring much-needed refreshing of my skills 🙂

As far as my older posts are concerned, the will stay available on the old google sites platform, as I don’t think it is good practice to move them here. The effort with recreating them, as well as updating all the links in previous posts to Facebook, is not quite worth the effort. That combined with some of the limitations on what is possible with google sites, made me believe that this is the best way to go, Old posts stay on old site, and new stuff goes here.

Feel free to tell me what you think about that in the comments below 🙂