IoT

I am starting an IoT project and wanted to share a little bit.  IoT or the Internet of Things is defined on Wikipedia as “the network of physical objects—devices, vehicles, buildings and other items embedded with electronics, software, sensors and network connectivity—that enables these objects to collect and exchange data. The Internet of Things allows objects to be sensed and controlled remotely across existing network infrastructure, creating opportunities for more direct integration of the physical world into computer-based systems, and resulting in improved efficiency, accuracy and economic benefit;”.  

To learn more I would recommend the book The Internet of Things Do-It-Yourself at Home Projects for Arduino, Raspberry Pi, and BeagleBone Black by Donald Norris. It provides an in-depth technical overview of concepts as well as some projects that can be built. The projects in the book are not particularly exciting but they do a good job at illustrating concepts and methods utilised.

Book

So I am going to be using a Raspberry Pi2 running Windows 10 IoT core, which will be communicating with some Arduino boards.

I am also looking at integrating with Azure Machine Learning to do some interesting things. 

I have not decided on many elements of the final project, but it will involve a robot.

rasp

On a side note, do not try to deploy Windows 10 IoT Core on a SD card using a Mac, it is a huge pain. My main computer I use at home (and for most of my development, blogging, video editing, etc.) is a MacBook Pro and in the end I gave up trying to get the deploy working and used my windows laptop which worked almost instantaneously.

Once I have decided exactly what I want to achieve and made some progress I will post more on this topic.

IoT

BITE SIZE ARDUINO – PIEZO BUZZER

Today we will have a look at how to connect a piezo buzzer to an Arduino and how to generate different audio signals with it. A piezo buzzer is a audio signalling device, it is the most basic electronic component by which to generate sounds at different frequencies.

A piezo buzzer has 2 connection terminals, one is connected to GND and the other to a PWM digital pin, for this example we will use pin 3.

piezo_bb

We will use the tone() function to generate tones at different frequencies, for an Arduino Uno the frequency rage is between 31 and 65535 Hz. Please note that the possible min and max frequencies differ between different models of Arduino boards. The tone() function takes 3 parameters, firstly the pin to use, then the frequency to use and lastly the duration to generate the tone in milliseconds.

Here is the code used:

#define PIEZO_PIN 3

void setup()
{
}

void loop()
{
     tone(PIEZO_PIN, 31, 1000);
     delay(500); // Pause between Tones 
     tone(PIEZO_PIN, 15000, 1000);
     delay(500); // Pause between Tones 
     tone(PIEZO_PIN, 30000, 1000);
     delay(500); // Pause between Tones 
     tone(PIEZO_PIN, 45000, 1000);
     delay(500); // Pause between Tones 
     tone(PIEZO_PIN, 65535, 1000);
     delay(500); // Pause between Tones 
     noTone(PIEZO_PIN); // Silence Tones
     delay(500);
}
BITE SIZE ARDUINO – PIEZO BUZZER

BITE SIZE ARDUINO – RGB LED

A RGB LED is a LED that can change the colour of the light it produces depending on which of the LEDs’ Connectors have current flowing through them. The LED has 4 connectors, one connector for red, one for green, one for blue and then finally an anode or a cathode, depending if the RGB is a common anode or cathode LED.

So what is the difference between common anode and common cathode?

Well a RGB LED is actually a combination of 3 LEDs, a red LED, a green LED and a blue LED. All LEDs have 2 connectors, an anode and a cathode. So depending how these LEDs are connected together determines if they share an anode or a cathode, thus common anode RGB LED or common cathode RGB LED. The Anode\Cathode leg can be identified as it is the longest leg on the LED. Below are 2 diagrams that illustrates the difference discussed.

Common Cathode:

Common Cathode_schemCommon Anode:

Common Anode_schem

How these 2 different RGB LEDs are connected to a circuit also differs, let us first have a look at a circuit that contains a common cathode RGB LED:

common cathode arduino_bb

Here is the code used with this circuit:

int redPin = 9;
int greenPin = 10;
int bluePin = 11;
 
void setup()
{
  pinMode(redPin, OUTPUT);
  pinMode(greenPin, OUTPUT);
  pinMode(bluePin, OUTPUT);  
}
 
void loop()
{
  setLEDColour(255, 0, 0);  // red
  delay(2000);
  setLEDColour(0, 255, 0);  // green
  delay(2000);
  setLEDColour(0, 0, 255);  // blue
  delay(2000);
  setLEDColour(255, 255, 0);  // yellow
  delay(2000);  
  setLEDColour(80, 0, 80);  // purple
  delay(2000);
}
 
void setLEDColour(int red, int green, int blue)
{
  analogWrite(redPin, red);
  analogWrite(greenPin, green);
  analogWrite(bluePin, blue);  
}

Now let us have a look at a circuit that contains a common anode RGB LED:

common anode arduino_bb

Code used with this circuit:

int redPin = 11;
int greenPin = 10;
int bluePin = 9;
 
void setup()
{
  pinMode(redPin, OUTPUT);
  pinMode(greenPin, OUTPUT);
  pinMode(bluePin, OUTPUT);  
}
 
void loop()
{
  setLEDColour(255, 0, 0);  // red
  delay(2000);
  setLEDColour(0, 255, 0);  // green
  delay(2000);
  setLEDColour(0, 0, 255);  // blue
  delay(2000);
  setLEDColour(255, 255, 0);  // yellow
  delay(2000);  
  setLEDColour(80, 0, 80);  // purple
  delay(2000);
}
 
void setLEDColour(int red, int green, int blue)
{
  red = 255 - red;
  green = 255 - green;
  blue = 255 - blue;

  analogWrite(redPin, red);
  analogWrite(greenPin, green);
  analogWrite(bluePin, blue);  
}

Although the circuits and code differ between the 2 types of RGB LEDs, the end results are exactly the same.

Try changing the values passed into the setLEDColour function to see what different colours can be created.

BITE SIZE ARDUINO – RGB LED

BITE SIZE ARDUINO – SERVO

Today we will have a look at how to connect a servo motor to an Arduino and how to control its movement.

A servo motor has 3 connector wires:
– A red wire for (+) power.
– A black wire for (-) power (GND).
– A orange, yellow or white cable for signal.

servo_bb

The signal wire of the servo must be connected to one of the analog pins on the Arduino, for the purpose of this example we will use A2. The red wire must be connected to the 5V pin on the Arduino and the black wire to the GND pin on the Arduino. As can be seen in the diagram above – a 100uF capacitor is connected between the 2 power terminals, the reason for this is that it prevents a voltage drop occurring in the circuit when the servo starts moving. A voltage drop can occur due to the fact that a servo consumes more power when starting to move then when it is already moving.

Below is the code used to rotate the servo to different positions:

#include "Servo.h" 

#define SERVO_PIN A2

Servo servoMotor;  

void setup()
{
    servoMotor.attach(SERVO_PIN);
}

void loop()
{
     servoMotor.write(0); // Rotate Servo to 0 Degrees
     delay(500); // Delay to allow Servo time to Move
     servoMotor.write(90); // Rotate Servo to 90 Degrees
     delay(500); // Delay to allow Servo time to Move
     servoMotor.write(180); // Rotate Servo to 180 Degrees
     delay(500); // Delay to allow Servo time to Move
}
BITE SIZE ARDUINO – SERVO

BITE SIZE ARDUINO – 3 PIN SNAP-ACTION LEVER SWITCH

Today we are looking at how to connect a 3 pin snap-action lever switch to an Arduino board and reading when it is pressed.

switch

The lever switch has 3 pins – the common terminal, the normally off terminal and the normally on terminal. If the switch is not pressed current will flow from the common terminal to the normally on terminal, however if the switch is pressed current will cease flowing from common to normally on and will start flowing from the common to normally off terminals.

For this example we will only utilise 2 of the terminals – the common and the normally off terminal.

The common terminal is connected to the 5V pin on the Arduino board and the normally off terminals’ connection is split:

lever Switch_bb

One leg connecting to the Arduino boards’ ground pin with a 10kOhm resistor in series.
The other leg connecting to a digital pin on the Arduino board, for this example digital pin 2.

Here is the code to determine when the switch is pressed:

#define LEVER_SWITCH_PIN 2
int pressSwitch = 0;
void setup()
{
Serial.begin(9600);
}

void loop()
{
pinMode(LEVER_SWITCH_PIN,INPUT);
pressSwitch = digitalRead(LEVER_SWITCH_PIN);
if(pressSwitch == HIGH)
{
Serial.println("Switch Pressed!");
delay(1000);
}
}
BITE SIZE ARDUINO – 3 PIN SNAP-ACTION LEVER SWITCH

Bite Size Arduino – Sharp IR Sensor

I have received a few messages asking me to give a bit more detail into the individual bits of the robots I have built so far. Thus I am starting this series of posts in which I will cover a single sensor\actuator integration to an Arduino board per post.

Today we will have a look at how to use a Sharp IR Sensor.

The IR sensor has 3 pins – 2 pins used to power the sensor and a signal pin that is used to communicate its reading.

sharpIR_bb

As shown in the image above, connect the red pin to the 5V pin on the Arduino, the black to the GND pin and the yellow signal pin to any one of the analog pins, for the purpose of this example we will use A4.

Below is the code used to get a reading from the sensor and print it out using the Serial.println:

#define IR_PIN A4
int IRDist = 0; 

void setup()
{
Serial.begin(9600);
}

void loop()
{
pinMode(IR_PIN,INPUT);
IRDist = analogRead(IR_PIN);
Serial.println(IRDist);
delay(1000);
}

I suggest using this code to experiment and see what readings get generated by placing the sensor at different distances from an obstacle.

Bite Size Arduino – Sharp IR Sensor

Bite Size Arduino – Analog vs Digital Pins

I will use the Arduino Bite Size posts to share small bits of information relating to the Arduino platform.

Today we will be examining the 2 main pin types that are present on Arduino boards. For this article I will be referring to the Arduino UNO R3, however all Arduino boards contain these pin types, just note that the quantities of the different pins do vary greatly between the different boards.

There are 2 main groups of pins on any Arduino Board, Analog and Digital pins.

Arduino copy

On this image the Digital pins are highlighted with a red block and the Analog pins with a yellow block.

So what is the difference between the 2 pin types?

Digital pins can read or write 2 possible values HIGH or LOW (1 or 0), whereas Analog pins can read a value between 0 to 1023 and write a value between 0 to 255.

Keeping this in mind it becomes apparent that the main purpose of these pins differ greatly. A Digital PIN can turn an LED on or off, whereas an Analog pin can turn the same LED on to a variety of brightness levels, not just 1. So if you want to simply turn an LED on and off a Digital pin would be the correct pin to use, but for a servo motor signal cable (that controls the movement of a servo motor) an Analog pin would be required as different values (0 to 255) determines how far the servo turns.

This logic also applies when it comes to sensors, for a switch that has 2 states (on and off) a Digital pin would be ideal, however for a IR range sensor it would not work as usually you would like to know a value that represents the distance the sensor is detecting. For this kind of sensor an Analog pin should be used.

So far this all seems pretty straight forward… Now let us consider that some Digital pins can “act” like Analog pins.

You will see by looking at the image on the Arduino above that certain of the Digital pins have ~ next to them. This indicates that the pin is capable of PWM or Pulse Width Modulation. PWM is a method of producing Analog results utilising a Digital means. This is achieved by the Digital pin switching between on and off at a high frequency and thus producing a square wave, where the time in the on state determines the width of the pulses created.

So what does this mean?

If analogWrite(255) is done to a PWM pin it will be in a permanent HIGH (On) state. Whereas analogWrite(0) would place the pin in a permanent LOW (Off) state.

So if analogWrite(127) is done to a PWM pin it will be in a HIGH (On) state roughly 50% of the time and LOW (Off) state for the remaining 50% of the time.  The Arduino PWM frequency is 980Hz, which means that the pin will switch between on and off (HIGH and LOW) approximately every 0.00051 seconds for the 50/50 example above.

Note however that although analogWrite works with PWM pins, analogRead does not. To read a PWM signal the pulseIn method should be used. We will cover the pulseIn method at a later time.

Bite Size Arduino – Analog vs Digital Pins

Gates, Build Gates not Bill Gates

The basis of building any logic circuit (even one as complex as a computer) comes down to logic gates. I will be discussing the 2 most basic logic gates today, an AND gate and an OR gate.

Logic gates are physical circuits that implements boolean functions, so to start let us look at the boolean AND and OR functions.

For all the examples below let us assume that we have 2 inputs: A and B, and that A and B both have 2 possible states: on or off, 1 or 0 in binary terms.

AND Function

An AND function requires both A and B to be in an “on” state to give a positive “on” result. (Just note the NAND function will give a positive “on” output when A and B are NOT both “on”. We will look at the NAND function and gate in detail at a later time).

Below is the state table for the AND function with inputs A and B as well as the resulting output: 

A B Output
0 (off) 0 (off) 0 (off)
1 (on) 0 (off) 0 (off)
0 (off) 1 (on) 0 (off)
1 (on) 1 (on) 1 (on)

OR Function

An OR function requires either A or B  (or both) to be in an “on” state to give a positive “on” result. (Just note the NOR function requires neither A or B to be in an “on” state to give a positive “on” result. Additionally the XOR function will give a positive “on” output only when A or B are “on” but NOT when both are “on”. We will also look at the NOR and XOR functions and gates in detail at a later time).

Below is the state table for the OR function with inputs A and B as well as the resulting output: 

A B Output
0 (off) 0 (off) 0 (off)
1 (on) 0 (off) 1 (on)
0 (off) 1 (on) 1 (on)
1 (on) 1 (on) 1 (on)

Now let us examine the gate circuits. (I have constructed both gates on a Adafruit Perma-Proto board shown in the picture below).

Gates

AND Gate:

Parts required:

  • 3  resistors (10k Ohm will do)
  • 2 push buttons (input A and B)
  • 2 BJT NPN transistors
  • 1 LED (output)

And Gate_bb

AND GATE

AND Gate Schematic

So by pushing the buttons in accordance to the AND state table above the outputs can be recreated. Because the 2 transistors are placed in series the circuit can only be completed when both button A and B are pressed, and thus the AND function is implemented.

OR Gate:

Parts required:

  • 3  resistors (1 x 10k Ohm and 2 x 660 Ohm resistors will do)
  • 2 push buttons (input A and B)
  • 2 BJT NPN transistors
  • 1 LED (output)

Or Gate_bb

OR Gate

OR Gate Schematic

So by pushing the buttons in accordance to the OR state table the corresponding outputs can be recreated. Because the 2 transistors are placed in parallel the circuit can be completed by pressing either the A or B button (or both). The circuit thus represents the OR function.

Just note the selection of resistor sizes are not cast in concrete, just pick a resistance high enough so your transistor does not get fried based on your power supply size (in my case a 9 Volt battery). I simply chose the resistors based on what I had available at the time.

Additionally if the role of the transistors in the circuit does not make sense to you please look at my earlier post (TRANSISTOR CRASH COURSE) that explains the functioning of transistors and their roles in circuits.

Gates, Build Gates not Bill Gates

Transistor Crash Course

Transistors
Today we will run through a brief overview of the two main variations of transistors commonly used in circuits, NPN and PNP transistors.

It is critical to understand the basic functioning of these transistors to understand any circuit of moderate complexity.

The first thing to note is that the basic function of transistors is to control the flow of current through a circuit.

Both the NPN and PNP transistors have 3 leads or legs, the Base, Emitter and Collector. The main difference between NPN and PNP transistors is how they control the flow of current.

If we imagine the current flowing through a circuit being water flowing through a pipe, a transistor would act like a faucet or tap. It can control the water going through it by opening or closing its valve.

And this is where the NPN and PNP transistors differ. A NPN transistor is closed by default, not allowing current to flow from its Emitter leg to its Collector leg, and will only allow current through when it is “opened” by applying a small amount of current to its Base leg, whereas the PNP transistor is open by default, allowing current to flow from its Emitter leg to its Collector leg, and applying current to its Base leg “closes” it thus stopping the flow of current between the Emitter and Collector.

Below are the schematic symbols for the 2 transistors:

Transistors

Where the numbers represent:

1 – Base
2 – Emitter
3 – Collector

On an unrelated topic, I use open source software called Fritzing for all my electronic schematics and drawings. It is a great piece of software and can be downloaded from www.fritzing.org.

Transistor Crash Course

Boards, Boards Everywhere

Currently there are numerous Arduino and Arduino compatible boards available, this post will do a quick comparison between 3 of these boards (Arduino UNO R3, Arduino Mega R3 and the Beetle which is a shrunk down version of the Arduino Leonardo) and then also a quick comparison between Arduino and Raspberry Pi Board Families.

The below picture illustrates the size difference between the Arduino boards:

Arduino Board Comparison

Here is a basic breakdown of the specifications of the three boards:

Specification UNO R3 MEGA R3 Beetle
Processor ATmega328P ATmega2560 Atmega32U4
Frequency 16 MHz 16 MHz 16 MHz
Dimensions 68.6 mm × 53.3 mm 101.6 mm × 53.3 mm 20mm X 22mm
Manufacturer Arduino Arduino DFRobot
Flash Memory 32kB 256kB 32kB
SRAM 2kB 8kB 2.5kB
Digital I/O Pins 14 54 3
Analog Pins 6 16 3

The Arduino UNO is a good starting point for anyone interested in beginning some Arduino builds, it is a good all round board for most projects and the only real constraint that I have ever run into with this board is running out of digital I/O and Analog input pins for larger projects.

The Arduino Mega overcomes this problem by offering more than double the pins. From a development and ease of use point of view it is almost identical to the UNO.

The Beetle has the least amount of pins exposed, 6 in total, 3 digital and 3 analog, so this can be a serious constraint on the nature of project it can be used for. On the other hand its tiny size makes it possible to use this board in projects where physical size is a constraint (Such as the Insect bot I posted about in an earlier post).   

Now lets look at the Raspberry Pi (Raspberry Pi 2 B to be precise) in comparison to the Arduino boards. Below are 2 Pictures showing its size in comparison to the Arduino UNO and Mega.

Pi and Mega Pi and Mega

Here is a basic specification breakdown for the Raspberry Pi (Raspberry 2 B):

Specification Raspberry Pi 2 B
Processor Cortex-A7
Frequency 900 MHz quad-core
Dimensions 85.60 mm × 56.5 mm
Manufacturer Raspberry Pi Foundation
Flash Memory MicroSD slot
SDRAM 1GB

So, which should you use? Arduino or Raspberry Pi? The answer is… It depends. Both boards have their strong and weak points. Let us look at some key distinguishing points between the two board families:

  • Price
    • The Arduino boards tend to cost a lot less than Raspberry Pi boards.
  • Memory
    • Raspberry Pi Boards have vastly more memory.
  • Processing Power
    • Raspberry Pi Boards again win this one by a huge margin.
  • Ease of Hardware interfacing
    • Arduino Boards make direct hardware interfaces with sensors and actuators much easier.
  • Online community
    • Both have a strong and thriving online community for help and support.
  • Development
    • Arduino is C only using the free Arduino IDE where as the Raspberry Pi has a variety of development options, including Python, Java, C, C++.

The Arduino makes hardware interfacing with sensors and actuators a great deal easier. However the Raspberry Pi offers vastly more memory and processing power. So which one to use depends very much on your projects’ requirements.

To put it simply there is no right or wrong choice, use what works for you or simply what you want to use.

This does however not mean that you cannot use both on a single project by setting up serial communication between the 2 boards. I am currently busy doing this on a project (see The Geek under the THE KILLER ROBOTICS FAMILY SO FAR! post).

Boards, Boards Everywhere