Introduction: A Simple Guessing Game - Python + XBees + Arduino

About: Electronics Engineer | Always in the phase of continuous learning

Here’s how the game works:
You have 4 tries to guess a letter between ‘a’ – ‘h’

-> Guess the right letter: You win! 🙂
-> Guess the wrong letter: Game over 🙁
-> Guess any other character outside of ‘a’ – ‘h’: Game over 🙁

Your Arduino will let you know the response by displaying it on a 16×2 LCD. The LEDs turn on in different colors if the guess is higher/lower or right or wrong.

Step 1: Diagram

Step 2: Hardware

What you'll need

Arduino board (Mega), 16×2 LCD display, 10K ohm potentiometer, XBee S2 radios (2), Breakout board for XBee module, XBee Explorer USB, LEDs (3), jumper wires, USB cable (A-to-B), 9V battery + connector and breadboard.

Step 3: Software

Step 4: XCTU

X1. Plug one of your XBee radios into an Explorer module and connect the module to your computer’s USB port. Launch the XCTU program.

X2. Click “Discover devices” and select the port to be scanned. Click “Next”.

Select the port parameters to be configured and click “Finish”.

Your radio module should be discovered under the “Devices discovered” list. Click “Add selected devices”.

X5. Click on “Read” in the Modem Configuration screen to see what firmware is on that radio. Each XBee radio should be running the newest firmware version of the ZigBee Coordinator AT or ZigBee Router AT. Update the radio firmware module by clicking “Update”.

X6. To get the radios talking, there are 3 important things we must check. The first is the “PAN ID”. The PAN ID is the network ID that these radios are going to talk on. We will be setting this to a unique value = 2019 (You can choose a value between 0 and 0xFFFF).

X7. Every XBee radio has a 64-bit serial number address printed on the back. The beginning (SH) or “high” part of the address will be 13A200. The last or “low” (SL) part of the address will be different for every radio.

X8. To make the XBee radios chat with each other, the addresses of the Router and the Coordinator are switched.

X9. Click on the “Write” button to program your radio. Once you’ve finished configuring your first radio with the required configuration software, gently remove that radio from the explorer module and carefully seat a second radio in the same module and repeat the above steps.

Step 5: Arduino IDE

A1. Connect the Arduino board (Mega) to one of your computer’s USB ports and open Arduino IDE.
A2. Select the correct serial port and board.
A3. Upload the sketch to the Arduino board by clicking on the Upload button.

NOTE: The sketch can be downloaded from here.

Step 6: Circuit

Plug the XBee radio configured as the Coordinator into your XBee Breakout board and connect it to the Arduino by following the schematic.

Step 7: Hardware Setup

H1. The Arduino board (Mega) is powered by a 9V battery.
H2. Plug the XBee radio configured as the Router into your XBee Explorer module and connect it to one of your computer’s USB ports.

Step 8: PyCharm

You can use any Python IDE, in this case I’m using PyCharm. Before you begin, make sure that the following prerequisites are met:

-> You have installed Python. You can get it from:
-> You are working with PyCharm Community.

Step 9: Creating a Python Script in PyCharm

P1. Let’s start our project: if you’re on the Welcome screen, click Create New Project. If you’ve already got a project open, choose File -> New Project.

P2. Select Pure Python -> Location (Specify the directory) -> Project Interpreter: New Virtualenv Environment -> Virtualenv tool -> Create.

P3. Select the project root in the Project tool window, then select File -> New -> Python file -> Type the new filename.

P4. PyCharm creates a new Python file and opens it for editing.

P5. Install the following package: PySerial (a Python library which provides support for serial connections over a variety of different devices)

To install any package in PyCharm:
File -> Settings.

P7. Under Project, select Project Interpreter and click on the “+” icon.

P8. In the search bar, type the package you wish to install and click on Install Package.

NOTE: Make sure the COM port number that is used in the Python code is that of the XBee Explorer module connected to the PC. The COM port number can be found in Device Manager -> Ports (COM#)

Step 10: Running Your Program