When Single Board computing meets AI

Divya Chandana
The Deep Hub
Published in
4 min readFeb 23, 2024

--

Math Game with Raspberry Pi & ChatGPT

published in Hackster.io

Image by Author

Introduction

Welcome to MathGPT! This is a special game that helps kids learn math in a fun way by using a Raspberry Pi and ChatGPT. Math problems are simplified into easy-to-understand games using objects like apples and toys.Instead of just numbers. Kids can see math in the things they play with every day. The game has different levels that increases in difficulty, encouraging children to improve their Math skills. Let’s dig into the details.

Components

Prerequisites for MathGPT Game Setup

Hardware Components

  1. Raspberry Pi 4B Computer: Equipped with a Broadcom BCM2711, quad-core Cortex-A72 (ARM v8) 64-bit SoC @ 1.5GHz.
  2. Traffic Lights Module: For providing visual feedback to the game’s users.
  3. I2C LCD 1602 Module: An IIC TWI Serial 16x2 display for showing questions and instructions.
  4. Breadboard: To facilitate connections between the Raspberry Pi GPIO pins and the modules.
  5. A stable power supply for the Raspberry Pi.
  6. GPIO connection wires suitable for interfacing the Raspberry Pi with the LCD and traffic light modules.
  7. A microSD card (recommended size 16GB or more) preloaded with the Raspberry Pi OS.

Software and Libraries:

  1. Raspberry Pi OS: The official operating system for Raspberry Pi, pre-installed on a microSD card.
  2. Thonny Python IDE: A Python Integrated Development Environment that comes with Raspberry Pi OS for writing and testing code.
  3. OpenAI Library
  4. RPI.GPIO Library: Raspberry Pi GPIO for interacting with the hardware components.

Circuit Setup Guide

Setting up your MathGPT game is straightforward and hassle-free. Here’s how to connect the components:

Image by Author

Connecting the LCD Display

  • Start with the I2C LCD 1602 Module.
  • Connect the GND pin on the LCD to a ground (GND) pin on the Raspberry Pi to complete the circuit.
  • Connect the VCC pin on the LCD to a 5V power pin on the Raspberry Pi to power the display.
  • Connect the SDA (Serial Data Line) and SCL (Serial Clock Line) pins to the corresponding SDA and SCL GPIO pins on the Raspberry Pi.

Voilà! Your LCD should now be connected and ready for action.

Connecting the Traffic Lights Module

  • The traffic lights module has four connections to make: ground, red, yellow, and green.
  • Attach the GND pin to a ground pin on the Raspberry Pi.
  • Connect the pins corresponding to red, yellow, and green lights to any available GPIO pins. These will control the lights.

Tada! Your circuit setup is complete. See, it’s very simple.

Powering On and Software Setup

  • Turn on your Raspberry Pi.
  • Once it’s booted up, open the Thonny Python IDE.

OpenAI setup

  • OpenAI Key: Ensure you have an OpenAI API key.
  • Craft Your Prompt: Design a prompt that generates simple math questions suitable for children.
  • Model Choice: Utilizing GPT-3.5 Turbo.
  • Token Count: A limit of 100 tokens is ample for this task, keeping responses concise.
from openai import OpenAI
key = "YOUR KEY"
client = OpenAI(api_key=key)
def generate_math_question(level):

prompt = f'Generate {scale[level]} Addition, Subtraction and Multiplication math questions with basic objects. Example questions include "1 apple + 1 apple" or "5 balls - 2 balls" or "5 balls * 2 balls". The questions should be straightforward and suitable for kids learning basic arithmetic operations.numbers should be below 100. Avoid complex scenarios and focus on simple, relatable objects to make the math game fun and engaging for children. Output each question as a string in the format "<number> <object> <operation> <number> <object>" and enclose them in a python list, such as ["3 pencils + 2 pencils","4 oranges - 1 orange","6 cookies * 3 cookies"].'
response = client.chat.completions.create(
model="gpt-3.5-turbo",
messages=[
{"role": "system", "content": prompt},
],
max_tokens=100,
temperature=0.5
)
return response.choices[0].message.content.strip()

Explaining the Game Design

A list of math questions are generated by GPT, based on prompts I’ve designed. Each question is parsed and displayed on the LCD screen, one at a time. As participants input their answers, the answer checker evaluates each input. For correct answers green LED will light up, while incorrect ones light up a red LED, as immediate feedback.

As User increases the levels the difficulty increases too. If the all the questions are answers correctly then game Levels up by displaying “Level up” message on the LCD display

Children interact with the game through an intuitive interface, where correct answers lights a green bulb while incorrect responses trigger a red bulb, providing immediate feedback without punitive scoring. Questions stays until the correct answer is given, promoting a supportive learning environment. As children conquer level one with all correct answers, they unlock subsequent levels for continuous learning and enjoyment, without any penalties or restrictions.

Added Legos to the project to enhance the appeal and immersive educational experience.

by DC

Conclusion

This innovative project offers a fun and accessible approach to mathematics, empowering children to visualize, mentally map, and solve equations while enjoying the learning process.

Project Links

[1] Code https://github.com/divyachandana/raspiProjects/blob/main/math_game_gpt.py

[2] Youtube

[3] https://www.hackster.io/divyachandana/mathgpt-a-fun-approach-to-learning-math-with-raspberry-pi-9f2379

--

--