Artificial Intelligence or AI has come a long way in recent years, and one of the most exciting applications of this technology is its ability to write code. In fact, AI tools like ChatGPT are now widely used to help generate code snippets, solve programming problems, and even write entire programs. But can AI really write code that works? In this blog post, I’ll share my experiment and observations on whether AI can generate functional, working Python code, using a real-world example of a digital photo frame project I’ve been working on.
My Experiment: AI Writes Code for a Digital Photo Frame
As part of my exploration of AI and its possibilities, I decided to test the coding capabilities of ChatGPT with a simple project: turning a Raspberry Pi and a Waveshare e-Paper display into a digital photo frame. The goal was to create a Python script that would display random images from a selected folder on the display.
However, instead of writing the code myself (which is what I usually do), I decided to let ChatGPT generate the code based on the test scripts provided by Waveshare. These test scripts help verify that the display works, but they are not what I needed. So I asked ChatGPT to clean up the test script, remove unnecessary parts, and turn it into a fully functional program that can display random images.
Results: Does it work?
To my surprise, ChatGPT delivered the code. Although it wasn’t perfect out of the box, it generated a working base. The script handled displaying images on the e-Paper display correctly, but there were a few issues that needed to be addressed, such as unnecessary test code and some minor logic tweaks.
What I learned:
- AI can generate working code, but needs some tuning
The AI-generated code was a good starting point, but it wasn’t perfect. AI can indeed help generate code that works in the right context. However, the code often requires some modification to fit the specific requirements of the project, especially in the case of non-standard needs. - Test code vs. production code
The test scripts provided by Waveshare were useful for checking the display. Keep in mind that test code often contains elements that are only used to check the hardware or functionality in isolation. To turn it into something more practical, additional modifications were necessary. ChatGPT was able to help with this by removing unnecessary parts and keeping only the relevant code. - AI is great for automation, but don’t rely on it completely
While AI can generate code quickly, it still requires the developer to have an eye for refinement. AI tools like ChatGPT are great at automating repetitive tasks and providing useful code snippets, but human input is necessary to make the code truly functional and optimized. I’ve learned that while AI can save time, it should be used as a complement to your coding skills, not as a replacement.
Tips for Writing Better Code with AI
Now that I’ve had a chance to experiment with AI-generated code, here are some tips for getting the best results from tools like ChatGPT or other AI coding assistants:
- Be specific in your requests
The more specific you are, the better the results will be. Instead of asking for “code to display images,” ask for “Python code to display
displaying random images from a folder on the 7.3-inch Waveshare e-Paper display.” The more context you provide, the more accurate the AI will be in generating the code you need.Break up complex tasks - If the project is complex, break it down into smaller tasks. For example, ask the AI to generate code for one part of the project first (e.g. displaying a single image), and then expand on that. This helps ensure that the AI generates cleaner, more manageable code that can be easily customized later. Review and refine
- AI-generated code is rarely perfect. Review it carefully to make sure it meets your needs. Look for redundancies, bugs, or performance bottlenecks. Don’t hesitate to remove unnecessary code or rewrite sections that need improvement. It’s important to remember that AI is a tool that’s meant to help you, not replace you entirely. Understand the code
- Although AI provides the code, it’s important that you understand how it works. AI can generate logic, but you should still understand what the code is doing so you can make informed decisions when tweaking or improving it. Provide feedback to improve AI results
- If the AI isn’t meeting expectations, provide more context or clarify your request in the next interaction. Iterate through the process to get better results every time.
Watch the whole process in action
In my video, I dive into this experiment and show you step-by-step how I used ChatGPT to clean up my test script and generate a working Python program for my digital photo frame. I also share some insights and tips so you can learn how to best use AI in coding.
If you want to see how AI can help with coding and how to improve AI-generated code, be sure to watch How I Got Working Python Code from AI to Build a Digital Photo Frame.
Checklist for getting better code from ChatGPT (more on this in the video):
- Provide specific information about what the code should do
- Provide programming language and version
- List required libraries, frameworks, or interfaces API
- Describe the inputs and expected outputs
- Define what is most important: readability, performance, etc.
- Ask for comments and clarifications if needed
- Provide context if this is part of a larger project
By the way It’s best to avoid pasting production code into ChatGPT and other similar tools unless you’re running such an AI server locally. Using public tools can involve the risk of disclosing sensitive data or code you don’t want to share. If you want to learn how to create your own local AI server that allows you to safely use such tools, be sure to read article on my blog or watch the video , in which I explain step by step how to turn even an old laptop into a local AI server.
Summary
Is AI really ready to write working code? In my experience, yes. AI can write working code. But as with any tool, it requires some human intervention to make sure the code is functional, optimized, and meets the specific needs of your project. AI can’t fully replace the creative and problem-solving skills that come with human programming, but it can certainly help speed up the process and help with more repetitive tasks.
So, while AI is a fantastic resource for generating code, it’s important to remember that it works best when used in conjunction with your own skills and expertise. Ultimately, it’s about you and the AI working together to create the best possible code.