Hacking a computer using Arduino. Introducing Arduino’s basic functions…

Electronics and safety

Please remember that electronic parts can be very sensitive: static electricity can be fatal to your Arduino and many other small computing parts, so wear anti-static wristbands and try not to shuffle your feet. Equally, though, remember that electronic parts can be dangerous to you – they often have lead in them, so make sure you wash your hands thoroughly after working with your kit!

Putting together little circuits is great fun, but please be careful!

Video

Wrapping Up

Hopefully everything worked out for you. If so (and maybe even if not), this has been a good way to cut your teeth on several subjects at once:

  • Alexa
  • Embedded programming
  • The ESP8266 chip
  • The Arduino IDE

Also of course, you have the maybe slight convenience, of being able to turn on/off your tv by voice command.

Why the Hack?

Why is this a hack, and not part of the basic API for the Alexa? After learning how to develop my first Alexa skill, all I really wanted to know was “how can I just send a command directly from the Alexa to another device on the network?” It’s a shame that Amazon hasn’t exposed a full-fledged API for communication between the Alexa device and other objects on the local network, without going through the “skill” or “smart-home” paradigm (wherein everything must be sent to AWS before doing anything), but they just have not.

Try to Take It Further

Try a suite of remote control commands to more fully control your tv, such as changing the channel and controlling volume. Test the limits of the chip by seeing how many different commands you can listen for on one ESP8266 (hint: the number barely breaks double digits, without some very clever programming). If you’re good with hardware, try controlling other devices not through IR, by wiring them directly to ESP8266 chips; like lighting and such. Reinvent the wemo!

Related:

Arduino: Using the Serial Monitor

The serial monitor is perhaps the most important debugging tool for Arduino programs. See the “Hello, World” program as an example:

To see “Hello, World” on your computer, open the Arduino application, choose the proper board (under tools -> board), and open the serial monitor. The example initializes serial communication with a speed of 9600 bits per second, so select 9600 after opening the serial monitor.

This guide does not cover using more complex sensors and devices with an Arduino, but a general outline of sending this data to a computer follows:

  1. Collect the data in some variable
  2. Print the variable to the Serial port
  3. Receive it with a desktop application

Exterminate, annihilate, destroy! (Yes, it’s a Dalek)

This one goes out to all the Doctor Who fans. Perhaps the Doctor’s most iconic enemy, the alien mutants in robotic shells known as Daleks are simultaneously terrifying and hilarious.

Who fan Andy Grove set out to build one, smartly combining the Raspberry Pi and Arduino:

I have used an Arduino Uno to monitor two ultrasonic sensors in the base of the Dalek and send the results over the USB serial interface to a Raspberry Pi which then plays an MP3 clip. I used a separate Arduino board to provide sound to light functionality to drive the dome lights.

I could have achieved the results I needed using just the Arduino or the Raspberry Pi but it seems to me that the Arduino is better suited to low-level functions interacting directly with sensors and motors and so on, whereas the power of the Raspberry Pi is that it is a fully functional Linux computer for tasks requiring more computational power, and where I can easily use existing skills to leverage the Internet later on. Eventually I plan to put motors in the dome and a webcam in the eye so that the Dalek can look directly at people that approach. I also want to have a Web interface to be able to control behavior.

Putting together the electronics was faster than building the bulk of the robot, made mostly of plywood, cardboard, and papier-mâché. Grove got the Dalek ready for this past Halloween, saying “[t]he construction took five months, with some time spent working on it almost every weekend.”

Enlarge /

Enlarge / “Would you care for some tea?”

The finished Dalek was absolutely worth the effort. Not only does it look like a Dalek, it’s also able to utter the evil robot species’ evil catch phrase:

For those of you who don’t watch Doctor Who and wonder why anyone would spend so much time building a Dalek, here’s your answer:

Dalek destruction.

Blinkenlights

The first thing to do with any programming project – hardware or otherwise – is usually something akin to a ‘hello world’ test: make your program send a simple message to the outside world to show that you have everything configured correctly.

The Arduino has several built-in LEDs, such as the TX and RX LEDs that flash when data is being sent or received. We’ll be using a special testing LEDs to show that the board is working OK. Getting Arduino to work on Linux can be a little tricky, because it uses Java. What’s more, it needs the official Sun Java rather than a clone.

If you’re using Ubuntu, make sure you have the Multiverse repository enabled, then install the packages sun-java5-jre, gcc-avr and avr-libc. Many users have trouble with braille support clashing with Arduino, so if you don’t need braille support you should remove the brltty package first – if you plugged in your Arduino board while brltty was installed, unplug it and try again once brltty is removed.

Once all the software is installed, run “sudo update-alternatives –config java” and select the number of the official Sun Java. That ought to be enough to get things working. If you’re not using Ubuntu or another Debian-based system, the same routing is required: Sun’s Java, gcc-avr and avr-libc, but you probably won’t have to worry about the update-alternatives system with other distros.

Before you start coding, you need to download the latest Arduino IDE – extract it to your desktop, then run the command “arduino”. If you have problems, try running it from a terminal so you can see any error messages. The first time you run the IDE, you’ll be asked to choose a place where your programs should be stored – a subdirectory of your home directory is fine.

The Arduino IDE is very simple, offering code highlighting, save and load, and not much more – but you can upload to your board straight from here.

Now that you have your Arduino software installed, go ahead and hook up the Arduino to your PC using a USB cable. If the board is working OK, the PWR (power) light will be on, and your test LED might already be flashing to show that something is on the board already.

In the Arduino editor, go to File > New to enter some new code. Code files in Arduinoland are known as ‘sketches’, and they are easy to get started with. We’re going to start with a simple project to make an LED flash on and off, and we want to show you the code and make sure it works before we look at how it works. Here’s the sketch:

Now go to Tools > Microcontroller and choose either atmega8 (if you have the ArduinoNG) or atmega168 (if you have the Diecimila). If you’re not sure, ignore this step – Arduino will check the CPU when it connects, and report an error if it finds the wrong CPU. If you get such an error, just choose the other option!

With the CPU configured, go to Tools > Serial Port, and you’ll see a list of possible USB devices. On Linux, chances are it’s /dev/ttyUSB0, so select that. OK, that’s all the configuration done – it’s time to upload your sketch to the board!

Before you try uploading to a board, make sure you select the correct serial port for your Arduino – it’s probably /dev/ttyUSB0 or something similar.

On the IDE menu bar, you’ll see an icon with a right-facing arrow on – that’s the Upload button. If you have a Diecimila, you can click that now and your program will be uploaded. Otherwise, you need to press the little reset button on the Arduino board first so that the Arduino is ready to accept a new sketch, then press the upload button.

The TX and RX lights should flicker for a second or so, followed by about five seconds of nothingness as your Arduino waits to see if any further instructions are going to arrive, and finally your little test LED should start flashing. Success!

You can take this little project one step further by taking an LED from your kit bag and plugging it into digital pin 13. If you haven’t used an LED before, you should look closely at it before plugging it in: note that it has two wires coming out, that one wire is longer than the other, and that one side of the plastic-coloured rim is flat.

The longer wire and the flat side are there to indicate polarity: the side with the shorter wire and the flat rim is negative. When plugging your LED into your Arduino board, make sure the positive wire goes into pin 13, and the negative wire goes into GND (Ground), and all being well you should seeing it flash in time with the test LED. If you get it the wrong way around, nothing will happen, but don’t leave the LED plugged in like that too long!

Tags

Leave a Reply

Your email address will not be published.