THINKING MACHINES 101 with Arduino

I) The Prelude

This post is written primarily for the “INTRO TO BUILDING THINKING MACHINES” class from Harrison Metal, but can be a general introduction on the same subject. As a course of this two hour class, we will be going over three major aspects of any thinking machines, which are the Sensing, Logic — Information Processing — Control: effectively the brains, and finally Actuation/Action.

We will be using Arduino based platform and work on projects that give us an idea of how these different aspects play out.

Of course there would be a lot more aspects that goes into machines in real world, and I will be oversimplifying the reality in a quite a few instances for the sake of this class. As such, take this post as a stepping stone. Each aspect gets more complex as you look into it further, but I assure you that it would be great fun to go about on that path. There are a lot of embedded systems and sensors that are available for us in this endeavor, and I will touch upon them at the end of the actual class.

II) The Introduction

We will be starting with the aspect that the end user of any machine would care about the most.

Actuation: In almost all cases, you would want a machine to perform a desired and intended behavior. This action could be moving an objects, giving answer to a question, turning on the fan, etc. All of these come under the umbrella of actuation/action.

For our project, the actuators we would use would be a moving a servo motor, displaying something on a LCD and an (arguably annoying) audible buzzer.

Pretty much every action that we working with will be requiring two kinds of input. One being a control signal (something that tells it what to do) and a source of power to actually do it (battery, energy, etc). In some cases, the above two signals can be combined into one, such as controlling the speed (varying the power over time) etc. This segues neatly to our next aspect of…

Logic, Information Processing and Control: This effectively is the brains of the entire operation. This is where the decision of what action needs to be taken is done, primarily based on the information gathered from the system and what we want the machine to do.

In our session, Arduino will be providing the platform on which we would be performing the logic. The actual hardware consists of microcontroller, flash memory, I/O pins, serial channels and a few other fancy sounding words. Think of it as a tiny computer on which you can code the logic.

Most of the lower lever work is abstracted out (such as interfacing with hardware, signal processing, protocols for communication), and we will just have to tell the Arduino what to do in the programming language of c++. This is where the programming aspect of this class comes in.

Reasonably, the action of the system should be based on what the Arduino “sees”. And the way the Arduino sees is by using a few nifty little devices which we categorize under sensors.

Sensors: These are effectively the sensory organs of the computer. For a computer, almost all sensor information is in the form of electrical signals and data, and we have figured out a good clean way of representing it in a way that both us (humans) and the computers can understand.
For example, the result of a camera for a human is an image, but to a computer is a matrix of numbers, and so on.

RFID detectors, Keypads are the primary inputs that we will be using in this session. These devices are interfaced with the Arduino by connecting them to the right I/O pins, and telling the Arduino to look for these devices in specific locations. Good people have written libraries (think of them as modules of code that does a specific task) that will help us get sensible data from these electrical signals seen by the Arduino.

Sensors are fundamental to any smart machine. A couple of examples in everyday life being a temperature sensor in our cell phones, cameras, gyroscopes which are the reason your mobile screen keeps changing orientation when you lie down.

III) The things of the internet

The Internet and Data: The relevance of connecting things to the internet and the benefits of gathering the data does not need any introduction. With smart mobiles connected to internet becoming ubiquitous, any device/machine/hardware is moving towards being connected to the internet for the ease to use, or to keep track of how things are going, or both. And we can do that from any part of the world, that connects to internet!! The data has a lot of value, particularly with the increasing use of machine learning in figuring out patterns to get results.

We will be touching upon this aspect and will also be showing a live usage of how to control a device connected to the internet using and storing and visualizing data. This part will be covered during the class directly.

IV) HANDS ON!!

Alright! It’s time to get to what we would actually be doing. The following will give you a glimpse of what we are trying to do, and we will follow it up baby and big steps using our Arduino.

  • The project: “DIY Smart, Simple and Secure Access System”
  • Objective: Using the Arduino Kit, build a system to grant access to an individual based on RFID and passcode authorization using a Keypad. The end goal is to move a Servo if both are authenticated.
  • User-Story:
    Note: Variants of this, as long as dual authenication is present, is valid
    ** An individual scans their ID card against the RFID sensor
    ** If the ID is not recognized, access is denied.
    ** The access denied message is diplayed on LCD with a buzzer
    ** If the ID is recognized, prompt user to enter passcode
    ** If passcode is recognized, grant access 
    ** Move servo motor on access
    ** If passcode is incorrect, ask user to re-scan RFID
  • Setup and things to bring: You will be working in randomly assigned groups of two. All of you will be provided the same Elegoo Most complete Arduino starter Kit 
    You will have to bring your laptop, and be open to the idea of installing Arduino tools. You will not have to bring anything else to the session. Power and access to internet will be provided (WiFi).
  • Timeline:
    10:00 :: 10:20 — Intro to machines, what we will be doing, goals.
    10:20 :: 10:30 — Install software + first test.
    10:30 :: 11:30 — BUILD MACHINE!!! (see section V)
    11:30 :: 11:45 — Evaluation
    11:45 :: 12:00 — Last words, downsides of shortcuts, software in real world, and show and tell of how the cloud and internet component will come into play.

Note: As with these classes in general, we will pace things accordingly and potentially modify the plans as we go through the evening. The objective is to learn, participate and have fun while doing it.

V) Project Instructions:

For a few who want to read up on Arduino and software before you actually come for the class, the following sections will give a taste of what we would do. Many pictures and code snippets are borrowed from here

* Breadboard Setup

Hardware Used: Solderless Breadboard, Wires

Steps: The vertical strips are called Power Rails and the horizontal strips are called Terminal Strips.

Power rails are connected vertically and Terminal Strips are connected horizontally.

  1. Make connections from Power rails to GND and 5V pin of Arduino board (Fig 0)
Fig 0 Breadboard with Arduino

* Setup Arduino Web Editor

  1. Go to https://create.arduino.cc/
  2. Create an Arduino account (if you don’t have one already)
  3. Log in the Arduino Web Editor
  4. Follow the steps to install Arduino Web Editor Plugin
  5. Go to examples section (See Fig 1) -> Basics -> “Blink” example and load it onto the Arduino.
  6. Click here for a reference to how to do it.

It looks something like the follows.

Fig 1: Arduino Web Editor

Timecheck: If you haven’t loaded this program 10:30 am, call the instructors.

* LED

Next up we will actually set something up on the breadboard to make a circuit.

Hardware: Arduino board, LED

Arduino Pins : GND and 13 (see Fig 2)

Software: Blink.ino (under Examples → Basics )

Steps:

  • Connect the positive end (longer leg) of the LED to the 13 and negative end to GND on Arduino board. Do NOT get the orientation incorrect
  • Upload the Blink.ino code
  • Check if the LED is blinking
Fig 2 Arduino with LED PIN connection.

Timecheck: 10:35 am

* SERVO

Hardware Used : Arduino Mega 2560, Breadboard, Servo , Wires

Arduino Pin Used — 2

Steps: Servo has 3 wire — brown is the ground wire, red is the power wire and orange is the signal wire (see Fig 3)

  1. Connect the power wire to 5V on the Breadboard
  2. Connect the ground wire to GND on the Breadboard
  3. Connect the signal wire of servo to Pin 2 in Arduino
Fig 3 servo motor (bottom) with Arudino

The following is the piece of code that you need to be uploading to the arduino to verify that it is indeed working. Any yes, you can copy paste this code.

//www.elegoo.com
//2016.12.08
#include <Servo.h>
Servo myservo; // create servo object to control a servo
// twelve servo objects can be created on most boards
int pos = 0; // variable to store the servo position
void setup() {
myservo.attach(9); // attaches the servo on pin 9 to the servo object
}
void loop() {
for (pos = 0; pos <= 180; pos += 1) { // goes from 0 degrees to 180 degrees
// in steps of 1 degree
myservo.write(pos); // tell servo to go to position in variable ‘pos’
delay(15); // waits 15ms for the servo to reach the position
}
for (pos = 180; pos >= 0; pos -= 1) { // goes from 180 degrees to 0 degrees
myservo.write(pos); // tell servo to go to position in variable ‘pos’
delay(15); // waits 15ms for the servo to reach the position
}
}

Notice how you have a include, setup and loop structure. Setup is where you tell arduino where all the hardware to interface is, and loop is what runs continuously (in a loop) to perform the action.

Timecheck: 10:40 am

* ACTIVE BUZZER

Hardware Used : Arduino Mega 2560,Breadboard and Buzzer , Wire

Arduino Pin Used : 3

Steps : Active Buzzer has 2 wires — brown is the ground wire and red is the power wire (Fig 4)

  1. Connect the power wire to 5V on the Breadboard
  2. Connect the ground wire to GND on the Breadboard
Fig 4 Active buzzer (bottom right) with Arduino
Timecheck: 10:45 am
//www.elegoo.com
//2016.12.08
int buzzer = 12;//the pin of the active buzzer
void setup()
{
pinMode(buzzer,OUTPUT);//initialize the buzzer pin as an output
}
void loop()
{
unsigned char i;
while(1)
{
//output an frequency
for(i=0;i<80;i++)
{
digitalWrite(buzzer,HIGH);
delay(1);//wait for 1ms
digitalWrite(buzzer,LOW);
delay(1);//wait for 1ms
}
//output another frequency
for(i=0;i<100;i++)
{
digitalWrite(buzzer,HIGH);
delay(2);//wait for 2ms
digitalWrite(buzzer,LOW);
delay(2);//wait for 2ms
}
}
}

* LCD Display

Hardware Used : Arduino Mega 2560, LCD Display, Breadboard, Wires, Potentiometer (to adjust the brightness of the screen)

Arduino Pin Used : 7,8,9,10,11,12

Steps :

  1. Connect the LCD on the breadboard (Fig. 5)VSS: A pin that connects to ground
    VDD: A pin that connects to a +5V power supply
    VO: A pin that adjust the contrast of LCD; 
    Connected to the potentiometer
    Additionally potentiometer needs to be connected with GND and +5V power supply
    RS: A register select pin that controls where in the LCD’s memory you are writing data to.
    Connected to Pin 7 on Arduino
    R/W: A Read/Write pin that selects reading mode or writing mode 
    Connected to GND
    E: An enabling pin that, when supplied with low-level energy, causes the LDC module to execute relevant instructions. 
    Connected to Pin 8 on Arduino 
    D0-D7:Pins that read and write data
    D0-D3 — no connection
    D4-D7 — Connected to Pin 9–12 on Arduino
    A and K: Pins that control the LED backlight 
    A is connected to +5V power supply and K is connected to GND
Fig 5 LCB display (top), connected to Arduino (bottom) via breadboard (middle)

You can use the following code and do a test run

/*
LiquidCrystal Library - Hello World
This sketch prints "Hello World!" to the LCD
and shows the time.
* wiper to LCD VO pin (pin 3)
http://www.arduino.cc/en/Tutorial/LiquidCrystal
*/
// include the library code:
#include <LiquidCrystal.h>
// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(7, 8, 9, 10, 11, 12);
void setup() {
// set up the LCD's number of columns and rows:
lcd.begin(16, 2);
// Print a message to the LCD.
lcd.print("Hello, World!");
}
void loop() {
// set the cursor to column 0, line 1
// (note: line 1 is the second row, since counting begins with 0):
lcd.setCursor(0, 1);
// print the number of seconds since reset:
lcd.print(millis() / 1000);
}

Timecheck: 10:50 am

* RC522 RFID Test

We will use the RFID sensor, and RFID + Arudino setup(Fig 3)

Hardware Used: Arduino Mega 2560, RFID Sensor, 7 Wires

Arduino Pin Used: — SDA = 53 ; RST = 5 , SCK = D52 ; MOSI = D51; MISO = D50; GND and 3.3 V

The names of the pins are visible on the RFID sensor that you have.

Fig 6 RFID sensor (bottom left), with RFID (bottom right) and Arduino (top)

Once you actually have this setup, run the following code to get it working

/*
PINOUT:
RC522 MODULE Uno/Nano MEGA
SDA D10 D9
SCK D13 D52
MOSI D11 D51
MISO D12 D50
IRQ N/A N/A
GND GND GND
RST D9 D8
3.3V 3.3V 3.3V
*/
/* Include the standard Arduino SPI library */
#include <SPI.h>
/* Include the RFID library */
#include <MFRC522.h>
/* Define the DIO used for the SDA (SS) and RST (reset) pins. */
#define SDA_DIO 9
#define RESET_DIO 8
/* Create an instance of the RFID library */
MFRC522 MFRC522(SDA_DIO, RESET_DIO);
void setup()
{
Serial.begin(9600);
/* Enable the SPI interface */
SPI.begin();
/* Initialise the RFID reader */
MFRC522.PCD_Init();
}
void loop()
{
/* Has a card been detected? */
if (MFRC522.PICC_IsNewCardPresent())
{
/* If so then get its serial number */
MFRC522.PICC_ReadCardSerial();
Serial.println("Card detected:");
for (byte i = 0; i < MFRC522.uid.size; i++)
{
Serial.print(MFRC522.uid.uidByte[i] < 0x10 ? " 0" : " ");
Serial.print(MFRC522.uid.uidByte[i], HEX);
}
Serial.println();

Serial.println();
}
delay(1000);
}

You will have to open up the serial monitor tab to see results of this on your online editor (Fig 7). You should be seeing data coming out of the arduino here.

Fig 7 Serial Monitor tab on Arduino Web editor

Time check: 10:55 am

* Matrix Keypad

Hardware Used : Arduino Mega 2560, 4x4 Matrix Keypad , Wires

Arduino Pin Used : A0-A7

Steps: Although this keypad has 16 keys (0–9, A-D, *, #) but it has only 8 output pins. Pin 1–4 are the rows and Pin 5–8 are columns of the matrix.

  1. Connect the Keypad pins of the Arduino board
  2. Pin 1 = A0, Pin 2= A1, Pin 3= A2, Pin 4= A3, Pin 5= A4, Pin 6= A5, Pin 7 = A6, Pin 8 = A7
Fig 8 Keypad (top left) with Arduino
//www.elegoo.com
//2016.12.9
#include <Keypad.h>
const byte ROWS = 4; //four rows
const byte COLS = 4; //four columns
//define the cymbols on the buttons of the keypads
char hexaKeys[ROWS][COLS] = {
{'1','2','3','A'},
{'4','5','6','B'},
{'7','8','9','C'},
{'*','0','#','D'}
};
byte rowPins[ROWS] = {9, 8, 7, 6}; //connect to the row pinouts of the keypad
byte colPins[COLS] = {5, 4, 3, 2}; //connect to the column pinouts of the keypad
//initialize an instance of class NewKeypad
Keypad customKeypad = Keypad( makeKeymap(hexaKeys), rowPins, colPins, ROWS, COLS);
void setup(){
Serial.begin(9600);
}

void loop(){
char customKey = customKeypad.getKey();

if (customKey){
Serial.println(customKey);
}
}

Once you run this code on the arduino, you will have to open up serial monitor (Fig 7), and you should be able to see the keys you press on the keypad using the monitor.

GET STARTED!!

Okay! Now you are have all the tools you need to finish the project based on the user story. You will have to use some control loops (logic conditionals in c++) to do what you want. I’ll cover some of them here, but feel free to reach of to the instructors if you know what you want to do, but are not sure how to code it up. You have 30 minutes to finish the logic!

https://github.com/konduri/harrison_metal/blob/master/arduino/rfid_lcd_arduino.ino

code is here

Pictures and code reference:

One clap, two clap, three clap, forty?

By clapping more or less, you can signal to us which stories really stand out.