Ka Pa Lik.
← all writing

2023.07.20

Building an AI Plotter Bot.

Turning a CNC plotter into a GPT-4-powered drawing machine that turns text prompts into SVG line art.

GPT-4CNCRoboticsSVG

A CNC plotter draws by moving a pen on two axes. I had a small belt-driven plotter collecting dust. The idea: connect it to GPT-4 vision to convert text prompts → SVG → G-code → physical drawing.

Components

  • 2-axis belt plotter (CNC shield + GRBL firmware)
  • Raspberry Pi 4 as the controller
  • GPT-4 Turbo for prompt-to-SVG generation
  • Node.js pipeline script
  • svgToGcode library for path conversion

Prompt to SVG

const { OpenAI } = require('openai');
const client = new OpenAI();

async function promptToSVG(prompt) {
  const completion = await client.chat.completions.create({
    model: 'gpt-4-turbo',
    messages: [{
      role: 'user',
      content: `Generate a clean SVG line drawing of: ${prompt}.`,
    }],
  });
  return completion.choices[0].message.content
    .match(/<svg[\s\S]*?<\/svg>/)?.[0];
}

Send G-code to plotter

# Convert SVG to G-code
node svg2gcode.js output.svg > plot.gcode

# Stream to GRBL over USB serial
node stream.js plot.gcode /dev/ttyUSB0

GPT-4 Turbo is inconsistent at generating valid SVG. About 30% of outputs had syntax errors or coordinates outside the viewbox. I added a validation pass with an XML parser and a simple bounds check.

What worked well

  • Simple geometric prompts: 'mountain silhouette', 'face outline', 'leaf pattern'
  • Abstract geometric art: reliable and clean
  • Portraits: hit-or-miss but occasionally impressive

The full pipeline from text prompt to physical drawing takes about 90 seconds. Slow, but the output is physical and that makes it feel like magic.

Use a felt-tip pen, not ballpoint — the plotter lacks enough Z-axis pressure for reliable ballpoint contact.

Thinger.io for Small IoT Projects