Click here to Skip to main content
Click here to Skip to main content

Control Physical World Through Computer (Step by Step)

, 2 Nov 2009 CPOL
Rate this:
Please Sign up or sign in to vote.
This is a step by step description of how to start physical world controlling through your code. This article explains both hardware and software for this operation.
Relay Circle

Introduction

In this article, we will take the first step inside the physical computing world. Physical Computing, in the broadest sense, means building interactive physical systems by the use of software and hardware that can sense and respond to the analog world. In physical computing, we take the human body as a given, and attempt to design within the limits of its expression (Wikipedia definition).

Background

To be able to understand this article, these things must be clear:

1- Binary System

As we all know, the only understandable commands for any computer is the (On/Off) power commands which is represented as 0 and 1. And for those two commands, we have only three logical operators. They are (AND, OR and NOT).

  • And: 1 AND 1 is 1, anything else is equal to Zero
  • OR: 0 OR 0 is 0, anything else is equal to One
  • Not: Not 1 is Zero, Not 0 is One

Those three operators are implemented using transistors so they will be able to deal with electricity and give the result.

After that, we have started implementing many functions using those three operators. And collecting them in some electric circles called gates, using those only we are able to build what we call as IC which can perform more complex operations to the data, and for storage also using the concept of flip flops.

You don't need to understand all of this, but at least you must understand the general points of how we can build computer using only 0 and 1.

2- Numerical System

As we said in the previous section, we must represent our data using 0 and 1. We called it binary system.
So everything later must be converted to 0 and 1. Let us see how to represent our numbers (decimal number system), characters and symbols using this system.

For example, this number (110101):
When we try to extract it to our decimal system, we can say it is:
(2^0*1)+(2^1*0)+(2^2*1)+(2^3*0)+(2^4*1)+(2^5*1) = 1+0+4+0+16+32=53.

For letters and symbols, we will find some encoding system such as ASCII which represent any single characters in 8 bit, for example it represents number (2) in this way (011 0010), (A) is (100 0001) and (/) is (010 1111).

3- Computer Ports

To start dealing with any external device, you must use one of computer ports to send your signals. Here are examples of computer ports:

Serial Port

Serial Port

One of the oldest computer ports. It was used to connect external modem to the computer. It is sending 1 byte (8 bit) in each time.

USB Port

USB Port

The most famous computer port. Most of the new devices now are coming with USB port support. With USB you can connect up to 127 device, you will have hot-swappable property so you will be able to have (Plug-in and Play). And with up to 480 MB/second speed in USB 2.0.
USB comes with only 4 wires, two for VCC/Ground, and the others for sending/receiving data.

Parallel Port

Parllel Port

This is the port that we will use in our examples, because it is the easiest one to deal with for beginners.
You can find a very good article about it written by Levent Saltuklaroglu.

Anyway, we will try to explain it one time again here.
A parallel port consists of 25 visible pens:

Parallel Port
  • 1, 14, 16 and 17: Control Pins, you can use them for both input and output
  • 2-9: Data Pins, you can use them to send data outside (output)
  • 10-13 and 15: Status Pins, used for reading data (Input)
  • 18-25: Ground Pins

To start sending data through Parallel Port, you must have:

  • Kernel Mode Driver: such as inpout32.dll for windows users
  • We will select C# as a programming language. You can select any
  • Any device which we can show some data through, the simplest one is LED

Now just take one LED, connect its positive pin to port number 2, and its negative one to any ground, and then open your Visual Studio (any version), import inpout32.dll in this way:

[DllImport("inpout32.dll", EntryPoint="Out32")]

Then declare this function:

public static extern void Output(int adress, int value);

Finally, write some code like this in any button to open first pin:

Output(888, 1);

*** Hint: (888) is the Parallel Port Number, you can make sure from Device Manager->COM Ports->Parallel Port ->Resources-Resources Setting, you may find it as hexadecimal, just convert it using your windows calculator.
*** Another Hint: Sometimes your inpout32.dll will give you an error, use the one in the example.

4- Tools

To start dealing with hardware, it is better if you have some tools to help you, such as:

Breadboard

breadboard

It will help you to put your wires and pins; it contains connected line (5 pins for example) with the same value so you can read it from many places.

Adapter

Adapter

As we know, any digital device will need a VCC and Ground to work, any adapter will give you this, and it will be better if it comes with voltage controller (some devices need more or less voltage).

Regulator

Regulator

It is just to keep fixed voltage even if the voltage goes up, for example you can have 5V Regulator to give you 5V even if you use 9V battery.

Sample of Output Devices

  • Seven Segment: Represent Numbers using 8 LEDs
  • Buzzer: Alarm instead of light in LED
  • Screen: Advanced Screens using the same concept of seven segment
  • Stepper Motors: Controlling Movement in the motor using digital signals

Sample of Input Devices

  • KeyPad: Convert numbers or characters to binary digits
  • Sensors: Fire Sensors, light sensors, water... etc. which send custom signal when some physical event happen

Sample of Controlling Devices

  • Buffer: Allow Signal to move whatever it is if the controller pin is equal to (One)
  • Relay: The same as buffer, but it can deal with 110/240V
  • Microcontroller: Advanced Electronic Board, consists of multiple input, output and processing controls

Relay and Controlling Electric Devices

By the end of this part, you must be able to control any electric device in your home.
The general idea with any electric device is very simple, you have plugged cable to the power, if you cut the cable the device will be switched off, if you reconnect it again it will work.

Circle

What we will try to do is just to put some controls which will allow electricity to move or not depending on some value we will read from the computer.
This control as we said before can be Relay.

Relay

Example

There are many types of relay, each relay comes with its one datasheet, and we will choose one and explain using his datasheet.

Relay

In our relay, we have 2 pins for controlling, and 3 pins in each side, when the control is (One) it will move the power from pin (1) to (3) in each side, else it will connect pin (1) to (2).
This is how it will respond:

Relay

There are some other types. This is one sample of it:

Relay

Now, back again to our work, we will use the relay instead of switch, and it will take the commands from pin2 form parallel port as VCC, and any ground (pin 25 as example):

Relay

Now try to run your application again, and enjoy.

Just as a reminder again, open Visual Studio, import new inpout32.dll and write down this declaration:

using System.Runtime.InteropServices
[DllImport("inpout32.dll", EntryPoint="Out32")]
public static extern void Output(int adress, int value);

And to open the first data pin:

Output(888, 1);

For second pin type 2, third one is 4 and fourth is 8 and so on.

Now start implementing any routine for that or for any electric device, open it through modem by calling, it is now just regular programming.

Security

In the previous example, we were connecting out electric circle directly to our parallel port, which is not safe. We must use some security circle in the middle.

For example, we can use buffer gate between our parallel port and electric device in this way:

Relay

But we will face a problem that the output ampere from buffer will not be enough to deal with the relay, so we will add transistor to pass the power, and we will take outside VCC source to the relay:

Relay

Simplify

As we observed in the last security circle, we will have a lot of wires. And in real application, it will be very difficult to handle all of this in a safe way.
As a solution, electronic manufactories provide us something called (Parallel Port Interface).

Relay

This board consists of 8 relays with security circle, it will need only one General VCC and one Ground, and controls for each relay. It will work directly.

Algorithms

Sometimes, we will need to deal with more than 8 devices.
For that purpose and other needs, we need to start reading about other gates.
In this example, we will try to Control 8 devices using only 3 pins.
For this purpose we will use Decoder. This is chart:

Relay

Second Example, Driving Remote Control Car

What We Will Need?

  • Remote Control Car
  • Relay
  • Transistor and Buffer (for security purpose only)
  • Some wires

Start

Again we will use the same concept; we will use Relay to control the power from the remote control. If we try to open and remote, we will find this structure:

Remote

If terminal (1) touches up terminal (2) it will go forward, (5) for left and so one.
That is what we can do using Relay. See this figure:

Remote

And that is all.
Now, start implementing your application to close and open pins. The only new thing is if you want to use keyboard keys, make keyPreview property of form to (True). And write down this code in Form_KeyDown event:

if (e.KeyValue.ToString() == "37")
// left
else if (e.KeyValue.ToString() == "38")
// up
else if (e.KeyValue.ToString() == "39")
// right
else if (e.KeyValue.ToString() == "40")
// down

There are more complex remotes which allow you to control speed, break,... etc. But the general idea is the same.

More Applications

Introduction to Robots

In this part, we will try to explain simply what the meaning of robot is. And how can we start implement small robot.

First of all, the robot is not only this kind of machine which is very similar to human body and face, the Robot is any machine with those 5 parts:

  • Body structure: Whatever this structure is, it is very important because it will affect the operation that it should perform
  • Muscle: Motors as an example
  • Sensor system: Instead of eyes and ears in humans, it maybe any kind of sensors or cameras
  • Brain: The group of functions which will handle the robots while performing its operation, it could be Microcontroller, or for simple use it can be your computer with cables (or wireless) commands to your robot
  • Power supply: Battery instead of food or whatever in humans

Start Work

Our simple robot mission is to detect the blue line on the ground and move on it.

We can use the same RC car that we have, we can use its body and motors as our body structure, muscles. And its battery as power supply.
Bring any webcam and put it in the front of the car, it will be our sensor system.

In our application, we will have a simple Image Processing Code to read the video from webcam, read image by image from it, and detect the blue line, depending on that it will start driving right or left.
And that is our brain.

That was the simplest robot, it is good as a beginning.

This is an advanced version, with microcontroller instead of connecting to computer, and motional camera.

Attached Files

The demo of this application is based in the application provided in this article
But with more features, you can open or close by the pin checkbox. You can also run a timer which provides different scenarios of opening/closing pins. And if you have a seven segment, you can use it to count, it contains seven segment equations.
And it is also contains the new inpout32.dll library.

Conclusion

If you find any mistake, please inform me, and sorry for any mistakes. I am not an electronics specialist.

Thanks. That is all Smile | :)

Ahmed Gamal - Web Developer

This article have been provided before in Arabic here.

History

  • 31st October, 2009: Initial version

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

A-Gamal
Web Developer ITitude
Egypt Egypt
Arabic Blogs and Forums:
www.AhmedGamal-space.blogspot.com
www.AhmedGamal-technical.blogspot.com
www.Fci-H-Ar.blogspot.com
www.Vb4Arab.com
 
English Blog:
www.AhmedGamal-Net.blogspot.com
www.Fci-H.blogspot.com

Comments and Discussions

 
GeneralMy vote of 5 PinmemberOmar Khaled28-Aug-13 1:27 
GeneralMy vote of 5 Pinmemberhari1911324-Oct-12 17:01 
GeneralMy vote of 5 PinmemberVitaly Tomilov10-Jun-12 9:20 
GeneralNice but .. PinmemberABABSA27-Apr-11 9:51 
GeneralMy vote of 5 Pinmemberring_018-Apr-11 22:11 
GeneralMy vote of 5 Pinmemberthatraja26-Sep-10 21:09 
GeneralSo far so good PinmemberYves16-Nov-09 8:25 
GeneralMy vote of 1 PinmemberBarrRobot9-Nov-09 4:18 
GeneralGood start Pinmemberund3rtak3r9-Nov-09 2:13 
GeneralRe: Good start [modified] PinmemberA-Gamal9-Nov-09 11:09 
GeneralParallel port not terribly common Pinmembersupercat92-Nov-09 14:25 
GeneralRe: Parallel port not terribly common PinmemberA-Gamal3-Nov-09 0:33 
Generalnice article Pinmemberemerging9-Nov-09 0:41 
GeneralRe: Parallel port not terribly common PinmemberThomasManz9-Nov-09 8:04 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.1411019.1 | Last Updated 2 Nov 2009
Article Copyright 2009 by A-Gamal
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid