Python with Arduino LESSON 4: Expanding your Virtual World

In this lesson we will expand the virtual world we created in Python with Arduino LESSON 3. We will be creating a virtual world that will track a simple scene in the real world. In this project, the virtual world will track both the position and the color of a target in the real world. This lesson requires that you have the Python software and libraries installed, which we explained in LESSON 2.

Arduino Circuit
This is our circuit with the HC-SR04 ultrasonic sensor and the TCS230 Color Sensor

This Lesson will be a bit more involved, and I will take you through it step-by-step. I will need to break things into two parts. In today’s lesson we will cover the Arduino side. We will develop the software that will measure distance and color, and then send those numbers over the serial port. Then in tomorrows lesson, we will develop the Python software to create a really cool virtual graphic to display the data in a virtual world.

For this project you will need the HC-SR04 ultrasonic sensor, the TCS230 Color Sensor, the Arduino Microcontroller, and some male/female jumper wires to connect to the color sensor.

The Ultrasonic Sensor can be attached per the schematic below:

Ultrasonic Sensor Circuit
Simple Circuit for Measuring Distance

Detailed tutorial on using this sensor was described in Arduino LESSON 18, so we will not go through all the details of using the sensor here. Review that lesson if you need more help. Key point here is to connect it as seen in diagram above.

You will also need to connect up the Color Sensor.

Connecting the Color Sensor to the Arduino

Color Sensor Pin
Arduino Pin
S1 5V
S2 pin 7
S3 pin 8
OUT pin 4

Use of the color sensor was described in detail in Arduino LESSON 15.  You should be able to develop to write the software yourself based on earlier lessons to make measurements from both the Color Sensor, and Ultrasonic Sensor, but if you get stuck, you can glance at my code below. Again, it is important for you to write your own code and not copy and paste mine. Mine is just a reference if you get stuck.

The key point to notice with this code is the print statements, summarized below:

 Notice that we are printing  our color strengths and distance on one line separated by commas. It is important to note the order of the data. When we read this in Python, we will read it in as one line of text, and then we will parse it into its individual values. So, we must make note and remember the order the data is arranged in in this line.

Remember when you have your python program reading this data, you must have your serial monitor closed. For now though, run your program and look at the serial monitor to verify you are getting correct data in the expected format.

In the next Lesson, LESSON 5, we will build the Python program to create a virtual world from this data.

5 thoughts on “Python with Arduino LESSON 4: Expanding your Virtual World”

  1. May I share two things I did here:

    1) I don’t have that colour sensor and no compelling reason to buy one (not that I really need compelling reasons to buy any of this stuff). So, I just used Arduino’s “random()” command to generate three values for RG&B.

    2) I bought a pair of Pololu Wixels a while back. ( These provide a wireless USB connection, just as with a mouse and the usb dongle thingy. So I have a wireless remote device now, with Arduino doing the serial prints of the colours through software serial and the Wixel pair rather than the USB cable. I used soft serial for the Wixel so I can still have the Arduino connected by USB cable for sketch uploads. One Wixel is with the Arduino, listening on soft serial on pins 2 and 3; the other is plugged into my laptop just like a mouse dongle. Laptop saw that Wixel as com13, so just had to change the Python code to talk on that port not the Arduino one.

    The Wixel on the Arduino talks wirelessly to its buddy which is on the laptop. Pololu claim the Wixels can talk over 50′ (~15m) indoors.

  2. Oops, PS to my above:

    I forgot to say that the Wixels’ IO is 3.3V not 5V, although the Wixel itself runs off 3-6V.

    I use a Uno, which is a 5V machine.

    So, when you send down the wireless USB to the remote Wixel, it talks 3.3V to the Arduino’s Rx. Arduino seems happy with that.

    BUT when the Arduino talks back, it Tx’s to the Wixel’s Rx at 5V which is ungood for the Wixel. But it’s easy to jig a 1K/2K divider to drop that to 3.3V for the Wixel. Page 25 of the Wixel pdf shows how to set that up; it’s dead simple but don’t forget to do it…..

  3. And my last note on this one!

    I decided to make the ultrasonic sensor move rather than have the target move, so mounted the sensor on the front of a robot chassis.

    Code in Arduino kills the motors when the distance is too small as the robot approaches the wall. My graphic is thus the stationary wall (rendered in that fancy brickwork material in Vp), with a cube to mimic the robot approaching the wall.

    Great fun this, thanks again for the tutorials…

Comments are closed.