A Magic Mirror: See Yourself in This Project

We’re moving from the Arduino to the Raspberry Pi! The Raspberry Pi can be used for SO many projects.

This project, what is a Magic Mirror? That. It’s a ‘mirror’ that you can look into to get information that will help you go about your day.

A fast introduction to the Raspberry Pi: It’s a micro-computer that runs on the Linux Operating System. It can also be a bit confusing if it’s your first time using it, so rather than me being your first introduction, here’s some better resources:

The Pi (and other Linux based micro-computers) give you so many possibilities and there is so much documentation that I don’t want to risk short-changing you guys.

Anyway, the mirror.

Parts:

  • Raspberry Pi 2 or better
    • Plus relevant cables
  • Old Monitor or TV
    • Try not to buy a new one. There is enough second hand monitors in the world that either you have a spare one, or someone you know if throwing one out soon
  • Frame
    • Optional, but will make the project look much nicer

Assembly:

Ok, everyone’s journey will be a little different with this because different monitors, different frames, different willingness to play with electricity and trip the fuses in the house multiple times… Don’t ask. (I know if I had people who read this I will get comments asking, so: Later, you will see why. Mistakes are always possible, especially when tinkering with new ideas and are more likely when you ‘work’ when you should be sleeping)

Step 1: I removed the frame from around the monitor, so that I could put my own frame around it. I did a lot of testing along the way to make sure I didn’t break everything.

Ok, it’s inside of a wood box I created, the wires are all as they should be and the frame is complete (Sorry I didn’t show the making of the wood box. It’s not that interesting. Saw + Wood = Cut)

Step 1.5: Forgot to take a photo of this step, but I got a piece of acrylic cut to the size of the monitor, then I ordered some one way mirror film to go on it. Follow video tutorials about using soapy water to get the best result and minimise bubbles while putting it on and that’s the ‘mirror’ part of your Magic Mirror.

How does that work? Well, anywhere that the monitor remains dark/black will remain reflective. Anywhere that is lit up will not look as reflective so it will show information. Simple as that.

Step 2: The Raspberry Pi also needs power, but the monitor I had didn’t have a USB out on it (well… it did, but it didn’t but… That doesn’t matter) So I hacked together a solution! (Remember the “Tripping fuses” thing I said earlier? This is why)

That is (was?) a high quality USB phone charger, capable of supplying over 2.4 amps. I ‘connected’ *cough* it to the power cord for the monitor in series so it could supply power to the Raspberry Pi without having to have multiple power cables running into the mirror. It’s *technically* safe. I only know this because I experienced multiple ways which it was wasn’t safe. I’m not going to go into any extra detail because:

I DO NOT WANT ANYONE TO DO WHAT I DID THERE! PLAYING WITH MAINS ELECTRICITY SHOULD ONLY BE DONE BY QUALIFIED ELECTRICIANS!

And I do have electrical qualifications.

Assembly Complete.

Code:

https://magicmirror.builders/

They did GREAT work, there is additional modules, you can remove the modules that it comes with (I removed the news module because I’m not American) and I customised the weather module (OpenWeatherMap is pretty fun)

So that’s about it for this project! Not much that can be said for how to do it, but a lot to be said for doing it.

And when you think about why you should do it, always remember.

Why? Because we can.

Our first project: A car of our own (Part 4 obstacle avoidance and temporary end)

First thing: This project is going to have a temporary end. The reason is because this is the final stage of the ‘basics’. We’ve already covered digitalWrite, serial communication and a few other things. This time we’ll be going in to digitalRead and using an LCD screen.

Will we return to this later? Probably, but for now at the end of this part we’ll have a car that can be controlled without fear of driving into stuff.

But again, here’s how I finished up:

Parts:

  • The ‘tank’ previously made
  • LCD screen
  • Two IR distance sensors

Assembly:

Starting to get a little confusing… So here’s the new additions:

  • Right IR sensor GND -> GND, VCC -> 5v, OUT -> D8
  • Left IR sensor GND -> GND, VCC -> 5v, OUT -> D7
  • LCD screen (with IIC) GND -> GND, VCC -> 5v, SDA -> SDA, SCL -> SCL

I’m particularly leaving the LCD’s SDA and SCL as that because those pins are sometimes different between different Arduinos so it is better to make sure with which one you have.

Also: Reminder about uploading code while the Bluetooth module is plugged in. You might encounter problems, so you might need to disconnect it while uploading code.

Code:

Anyway, here’s some Arduino code!

#include <Wire.h> // Comes with Arduino IDE
// Get the LCD I2C Library here:
// https://bitbucket.org/fmalpartida/new-liquidcrystal/downloads
// Move any other LCD libraries to another folder or delete them
// See Library "Docs" folder for possible commands etc.
#include <LiquidCrystal_I2C.h>
byte serialA;
int RF = 11;
int RR = 10;
int LF = 9;
int LR = 6;
int IRRight = 8;
int IRLeft = 7;
int valRight = 0;
int valLeft = 0;
LiquidCrystal_I2C lcd(0x27, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE); // Set the LCD I2C address, make sure 0x27 is your LCD's I2C address
void setup()
{
Serial.begin(9600);
pinMode(RF, OUTPUT);
pinMode(RR, OUTPUT);
pinMode(LF, OUTPUT);
pinMode(LR, OUTPUT);
pinMode(IRRight, INPUT);
pinMode(IRLeft, INPUT);
lcd.begin(20,4); // initialize the lcd for 20 chars 4 lines, turn on backlight
for(int i = 0; i< 1; i++)
{
lcd.backlight();
delay(50);
lcd.noBacklight();
delay(50);
}
lcd.backlight();
lcd.setCursor(0,0);
lcd.print("Let's start");
}
void loop(){
if (Serial.available() > 0) {
serialA = Serial.read();
Serial.println(serialA);
lcd.clear();
printAll(serialA);
Serial.println("top");
changeMotors(serialA);
delay(100);
while((Serial.available()>0)){
serialA=Serial.read();
printAll(serialA);
Serial.println("Bottom");
}
delay(100);
}
valRight = digitalRead(IRRight);
valLeft = digitalRead(IRLeft);
//When the IR sensors sense nothing, they return 1, so when they sense something they return 0. If their sum is less than 2 they sense something and must stop.
if (valRight + valLeft < 2) {
changeMotors(48);
delay(1000);
}
}
void printAll(byte b){
lcd.write(b);
Serial.print("Received the following: ");
Serial.println(b);
Serial.print("Serial.available is... ");
Serial.println(Serial.available());
}
void changeMotors(byte a){
printAll(a);
switch (a) {
case 48: //Stop on text 0 through serial
digitalWrite(LF, LOW);
digitalWrite(RF, LOW);
digitalWrite(LR, LOW);
digitalWrite(RR, LOW);
break;
case 49: //Forward on text 1 through serial
digitalWrite(RF, HIGH);
digitalWrite(LF, HIGH);
digitalWrite(LR, LOW);
digitalWrite(RR, LOW);
break;
case 50: //LEFT on text 2 through serial
digitalWrite(LF, LOW);
digitalWrite(LR, HIGH);
digitalWrite(RF, HIGH);
digitalWrite(RR, LOW);
break;
case 51: //RIGHT on text 3 through serial
digitalWrite(LF, HIGH);
digitalWrite(RF, LOW);
digitalWrite(LR, LOW);
digitalWrite(RR, HIGH);
break;
case 52: //Reverse on text 4 through serial
digitalWrite(LF, LOW);
digitalWrite(RF, LOW);
digitalWrite(LR, HIGH);
digitalWrite(RR, HIGH);
break;
}
}

Instead of multiple if statements for each IR sensor both are dealt with at once because the same thing is going to be done for both for now. “If you see something. Stop.”

You might notice the code is kinda neatened up as well. If you can read it and understand it you win a prize! That prize is the gift of knowledge.

So, hopefully this particular project has been interesting. Next up will be another interesting project. Which one will it be? I dunno, whichever one I can be bothered documenting. I have one that has scorch marks on it, another that is half finished and OH this one has food on it, yum!

Just remember when you build this project or when you’re about to build your next project.

Why? Because we can.

Our first project: A car of our own (Part 3 Manual Control)

One day, it would be good to let it do full self driving. For now, we’ll be working with manual control where your mobile phone is the controller.

Quick rundown of serial communication: It’s when you send data (communicate) to things using serial. USB (Universal Serial Bus) has become the most common lately, the different heads are just to connect to things of different sizes.

Parts:

  • The ‘tank’ previously made
  • Bluetooth module
  • Android phone with Bluetooth -> Arduino app installed

I made a (very simple) Android app that allows you to send data to Arduino via Bluetooth. Technically you can send data to anything that uses Bluetooth (Raspberry Pi for example) but the focus is with Arduino. I’ve released it 100% free, no ads and open source.

I’m going to be working on it and improving it over time, however you can find it on the Google Play Store here.

Assembly:

Another diagram!

Very similar, only addition is the Bluetooth module. To make it obvious how to wire everything up, here’s what you do:

  • VCC -> 5v
  • GND -> GND
  • RXD -> TXD
  • TXD -> RXD

Unless you are using an Arduino Leonardo or another that has multiple serial ports (if you don’t know for sure, assume you only have one serial port) you will need to disconnect and unplug the Bluetooth module completely whenever you are uploading new code to the Arduino.

Code:

Here is the code you will want on your Arduino:

byte serialA;
int RF = 11;
int RR = 10;
int LF = 9;
int LR = 6;
void setup()
{
Serial.begin(9600);
pinMode(11, OUTPUT); //RF
pinMode(10, OUTPUT); //RR
pinMode(9, OUTPUT); //LF
pinMode(6, OUTPUT); //LR
}

void loop()
{
if (Serial.available() > 0) {serialA = Serial.read();Serial.println(serialA);}

switch (serialA) {
case 49: //Forward on text 1 through serial
digitalWrite(RF, HIGH);
digitalWrite(LF, HIGH);
digitalWrite(LR, LOW);
digitalWrite(RR, LOW);
break;
case 50: //LEFT on text 2 through serial
digitalWrite(LF, LOW);
digitalWrite(LR, HIGH);
digitalWrite(RF, HIGH);
digitalWrite(RR, LOW);
break;
case 51: //RIGHT on text 3 through serial
digitalWrite(LF, HIGH);
digitalWrite(RF, LOW);
digitalWrite(LR, LOW);
digitalWrite(RR, HIGH);
break;
case 52: //Reverse on text 4 through serial
digitalWrite(LF, LOW);
digitalWrite(RF, LOW);
digitalWrite(LR, HIGH);
digitalWrite(RR, HIGH);
break;
}
Serial.print("Received the following: ");
Serial.println(serialA);
}

The switch looks at serialA and depending on what it is will do different cases. The reason for case 49 is because ASCII 1 gets translated to decimal 49 while being sent/received (I’m surprised it isn’t translated to 39 which is 1 in hex but whatever)

Anyway, by now you have your code uploaded, you have the app installed on your phone. Plug the Bluetooth module in and the little red light should start blinking on it. That shows it’s ready to be paired and connected.

Pair it with your phone (the pin should be 1234. If it isn’t, try 0000. If it still isn’t working, double check everything) and then open the app, find the Bluetooth module and then send data to it. Best to send 1 and watch your tank/car/whatever take off.

Our first project: A car of our own (Part 2 Movement)

A sneak peak at how this stage will finish:

Don’t worry, it’s spinning in place on purpose. It’s just easier to show off this way. You’ll understand why when you see the code later. This part will focus on uploading code and digital output.

Speaking of code… If you don’t already have the Arduino IDE installed, download and install it now.

Ok, so in our previous part, we did a bunch of planning, this time we’ll do building. What will you need?

Parts:

  • Arduino (Micro, Mini, Uno, doesn’t matter. I used a Micro)
  • H-Bridge IC chip (SN754410NE is what I used. There is others. It’s possible to use a shield, but using a single IC means it’s smaller over all)
  • Batteries (6v total, I used four 1.5v batteries)
  • Two motors (Geared or not, your choice. Mine are geared)
  • 5 volt regulator (Turns higher voltages down to 5 volts)
  • Body and wheels (This is all personal opinion. I’ve got a tank. So many options)

Assembly:

Time for a circuit diagram!

I made that with Fritzing which is a great tool.

Now let’s go through this! If you want to know how an H-Bridge controls the motors and why it’s needed, there are plenty of videos on it (in fact I probably should make one… But I’m a bit lazy) but basically wire up everything as shown there. The Arduino will control how the H-Bridge controls the motors which will control how the tank moves.

Code:

This is VERY simple code. This is simply to make it spin in place to make sure everything is working.

int RF = 11; //Right Forward pin
int RR = 10; //Right Reverse pin
int LF = 9; //Left Forward pin
int LR = 6; //Left Reverse pin

void setup()
{
pinMode(RF, OUTPUT); //RF
pinMode(RR, OUTPUT); //RR
pinMode(LF, OUTPUT); //LF
pinMode(LR, OUTPUT); //LR
}

void loop(){
digitalWrite(LF, LOW);
digitalWrite(LR, HIGH);
digitalWrite(RF, HIGH);
digitalWrite(RR, LOW);
delay(500);
}

Going through this bit by bit this is what we get:

First we define which pins are doing what. For example, pin 11 is tasked with making the right motor go ‘forward’  when it is on (high) and pin 10 (Right Reverse) is off (low).

In setup, we do just that. Set everything up. We tell the Arduino what each of the pins are doing. In this case all the pins are going to be OUTPUT pins. IE: they can turn on and off, either giving 0 or 5 volts. This only runs once whenever the Arduino boots up.

Then loop. This will loop forever. As long as the Arduino has power it will loop. So here, it will tell the pins “Pin 9, go low. Pin 6, go high. Pin 11, go high. Pin 10, go low.” It will then wait 500 milliseconds, then it will repeat the process.

Next up: More! Something… I don’t know the kids just woke up.

 

Our first project: A car of our own (Part 1 Planning)

The goal of this project is to build up our skills. While it would be fun for our first project to be to build a laser harp or a self playing and easily programmable guitar (both projects to expect later on), I’m going with something ‘easier’ but still awesome for our first project because this is potentially going to be the first project for a number of people.

Quick note about why I chose this as the first one: It will entail the basics of Arduino/robotics like digital output, digital input, serial read/write etc. so it’s simple and interesting.

Focuses: (What it is, how it relates to this project)

  • Reading circuit diagrams
    • Wiring
    • Not releasing the magic blue smoke
  • Digital I/O on Arduino
    • Controlling motors with an Arduino

For anyone who don’t know what the ‘magic blue smoke’ is, it’s when you fry an electric component. If you put too much voltage or current through it, it will release some blue smoke and the magic inside of the component that made it work is gone forever.

So let’s get started!

Planning:

What do we want to do? Build a small car of our own that we can control and can be upgraded.
Let’s break that down a little. It needs to: Drive, be controlled, not take up all the resources doing the basics and not crash into things when driving forward.

How will it drive? I’m personally a fan of the Arduino -> H-bridge -> two Motors set up. Where you control forwards, reverse, left and right by controlling the motors rotation. This handy diagram I drew (in paint…) will help describe what I mean. M = motors, C = caster so this design traditionally works on three points of stability.

The alternative option is two ‘drive’ wheels and one motor and two ‘steering’ wheels which are turned by a servo and three rods, this is what you see in the cars you drive. That option is more energy efficient when in use however the option I’m going with is easier to build and program for us.

How will it be aware of it’s surroundings? Because we’re not putting this on the public streets of $INSERT_YOUR_TOWN_HERE we only really need to worry about what’s directly in front of it so it doesn’t crash into things. We have a couple of options. Laser Distance sensors, Ultra Sonic distance sensors and Infra-Red distance sensor (which comes in ‘analog’ and ‘digital’ versions. So it will either tell you ‘this item is X distance away’ or it will simply reply HIGH or LOW depending on of there is something too close to it or not). The Laser is the most accurate, but also the most expensive, the analog Infra-Red is fairly accurate with a decent range without being too expensive, the Ultra-Sonic is cheaper but the cheapest and least informative is the digital IR sensor. Each of them have their benefits and their drawbacks (IE: laser is most accurate however is less accurate when being shone on items that are reflective and the cost is another drawback. Ultra Sonic has poor range) however for our purposes I’m going to be using the digital IR sensor ones because that’s what I have lying around and they fit the bill for what we need.

Thinking? The Arduino will do the thinking. It will take information it’s given (from us, from the sensors) and go from there. However it won’t know anything else about the world. For example unless we put a sensor on the wheels (an encoder for example), it won’t know if a wheel has stopped spinning

That about covers planning. Next post will be movement!

It begins

Welcome to Pd’s Workshop.

Who am I? I’m PdGeorge and this is my online workshop. This is my little place to teach people how to build robotics projects from start to finish, to show off projects I’ve made myself and if I get around to figuring out how to I’ll set up an online store to sell kits to make it even easier for people to follow along..

For any robotics project I make, it will come with a detailed walkthrough on how you can make it yourself alongside the planning process to help inspire your own projects.

Finally: Challenges! Keep an eye out for challenges. They will be open ended, no time limit and completion of them will result in prizes! (check out the Challenges page for more information)

However for now, always remember.

Why? Because we can.