element14 Community
element14 Community
    Register Log In
  • Site
  • Search
  • Log In Register
  • About Us
  • Community Hub
    Community Hub
    • What's New on element14
    • Feedback and Support
    • Benefits of Membership
    • Personal Blogs
    • Members Area
    • Achievement Levels
  • Learn
    Learn
    • Ask an Expert
    • eBooks
    • element14 presents
    • Learning Center
    • Tech Spotlight
    • STEM Academy
    • Webinars, Training and Events
    • Learning Groups
  • Technologies
    Technologies
    • 3D Printing
    • FPGA
    • Industrial Automation
    • Internet of Things
    • Power & Energy
    • Sensors
    • Technology Groups
  • Challenges & Projects
    Challenges & Projects
    • Design Challenges
    • element14 presents Projects
    • Project14
    • Arduino Projects
    • Raspberry Pi Projects
    • Project Groups
  • Products
    Products
    • Arduino
    • Avnet Boards Community
    • Dev Tools
    • Manufacturers
    • Multicomp Pro
    • Product Groups
    • Raspberry Pi
    • RoadTests & Reviews
  • Store
    Store
    • Visit Your Store
    • Choose another store...
      • Europe
      •  Austria (German)
      •  Belgium (Dutch, French)
      •  Bulgaria (Bulgarian)
      •  Czech Republic (Czech)
      •  Denmark (Danish)
      •  Estonia (Estonian)
      •  Finland (Finnish)
      •  France (French)
      •  Germany (German)
      •  Hungary (Hungarian)
      •  Ireland
      •  Israel
      •  Italy (Italian)
      •  Latvia (Latvian)
      •  
      •  Lithuania (Lithuanian)
      •  Netherlands (Dutch)
      •  Norway (Norwegian)
      •  Poland (Polish)
      •  Portugal (Portuguese)
      •  Romania (Romanian)
      •  Russia (Russian)
      •  Slovakia (Slovak)
      •  Slovenia (Slovenian)
      •  Spain (Spanish)
      •  Sweden (Swedish)
      •  Switzerland(German, French)
      •  Turkey (Turkish)
      •  United Kingdom
      • Asia Pacific
      •  Australia
      •  China
      •  Hong Kong
      •  India
      •  Korea (Korean)
      •  Malaysia
      •  New Zealand
      •  Philippines
      •  Singapore
      •  Taiwan
      •  Thailand (Thai)
      • Americas
      •  Brazil (Portuguese)
      •  Canada
      •  Mexico (Spanish)
      •  United States
      Can't find the country/region you're looking for? Visit our export site or find a local distributor.
  • Translate
  • Profile
  • Settings
Pi Chef Design Challenge
  • Challenges & Projects
  • Design Challenges
  • Pi Chef Design Challenge
  • More
  • Cancel
Pi Chef Design Challenge
Blog Automatic Dough Shaper - Blog #8 gcode and motors
  • Blog
  • Forum
  • Documents
  • Polls
  • Files
  • Events
  • Mentions
  • Sub-Groups
  • Tags
  • More
  • Cancel
  • New
  • Share
  • More
  • Cancel
Group Actions
  • Group RSS
  • More
  • Cancel
Engagement
  • Author Author: a_davis_22
  • Date Created: 21 Mar 2018 3:48 PM Date Created
  • Views 723 views
  • Likes 5 likes
  • Comments 1 comment
  • automatic dough shaper
  • github
  • gcode
  • a4988
  • diy cnc
Related
Recommended

Automatic Dough Shaper - Blog #8 gcode and motors

a_davis_22
a_davis_22
21 Mar 2018

As I was waiting for my new PSoC to come, I decided to take some time to learn about gcode. For a long time I've wanted to know how CNC's work and what gcode really is. I figured it was a list of instructions like this, "go to (3, 2)" but I learned it's actually more complicated than that. Gcode is the language sent from a computer that instructs a CNC or XY plotter or 3D printer where and how to move. Typically an instruction starts with a letter of the alphabet and is followed by numbers. There are usually multiple of these letter-number combinations per line (usually called a block). For example "G00 X0 Y0 Z0" which tells each axis to go to the origin. Each letter is called a word and the numbers next to it are the address associated with that word. G is the go word,  X controls the x-axis, Y controls the y-axis, and Z controls the z-axis. Every letter of the alphabet is used in gcode, but not all machines will use every letter. When a line of code is executed, it is not necessarily done left to right, it does the highest priority words first (like when doing math - you do multiplication before addition).

 

I figured that this would be a lot to code on my own and I could probably find some open source code I could use instead. As I looked around online, I came across PyCNC, which is a nifty program that actually uses python to do all the control. This program was developed so that a raspberry pi can control a CNC type of machine without a microcontroller!  The way it works is by using DMA (direct memory accessing) to bypass the operating system. It basically stores the instructions in RAM and then sends those instructions directly to the GPIO pins. This allows for exact control of things like stepper motors and it uses a simple programming language - python. Since I originally intended to control the whole machine with just the raspberry pi, I'm going to use this (if it will really work).

 

I was excited to clone the github repository for PyCNC and test it out! I ran the update/ upgrade commands (which you're supposed to do pretty often):

sudo apt-get update

sudo apt-get upgrade

And it took forever! Apparently doing this every couple of months is not frequent enough. After quite some time waiting, my raspberry pi said the SD card had been removed. I checked - it was still there and nothing had moved. But, unfortunately, my raspberry pi would no longer respond. Since I've been running my raspberry pi headless (I only have a laptop), I had to go over to my in-laws and use their monitor, keyboard, and mouse to set up my pi again. Fortunately, I learned that SD cards installed with NOOBS have a set-up menu that you can access on start-up by holding the shift key. This allowed me to easily re-install Raspian. I also learned that with the new Raspian distro, Pixel, the upgrade command is now "sudo apt-get dist-upgrade" and I thought maybe that was my problem. So I ran the update/upgrade commands again, this time using "dist-upgrade" and it got farther, but still crashed. So finally I decided that my program will work just fine without the upgrade, and I re-installed Raspian. It's important to enable things like SSH and VNC if you're going to operate headless, otherwise you'll get home and realize you have no way of accessing your pi. At home I connected my raspberry pi to my router via ethernet cable and then remotely accessed my pi using VNC. I used that to hook up my pi to WiFi so I don't have to sit next to the router every time I want to work on my project.

 

Finally I could clone the github repository! Since I have never used github before, I had to do some rummaging online to figure this out. Github is an online host for private and public code. It stores original code and then any revisions that get pushed up to it. That way if you mess up your code, you can always download an old version that you know works. Cloning a repository is like downloading the code to your own computer. On the github page for the code you want, just above the list of what is stored in that repository, on the right, is a button that says "clone or download."

image

This will give you a web address such as https://github.com/Nikolay-Kha/PyCNC.git . On the raspberry pi, from the terminal you make sure you are in the directory where you want this code and then write the command "git clone https://github.com/Nikolay-Kha/PyCNC.git" and then it will copy the code onto your pi.

 

PyCNC gives step, direction, and enable outputs, so you have to use an A4988 or DRV8825 driver board to interface with the stepper motors. Here's the pinout from the readme section.

Circuit nameRPi pin nameRAMPSv1.4 board pin nameNote
X stepGPIO21A0
X dirGPIO20A1
steppers enableGPIO26A2, A8, D24, D30, D38all steppers
Y stepGPIO16A6
Y dirGPIO19A7
Z dirGPIO13D48
Z stepGPIO12D46
E1 stepGPIO6D36reserve
E1 dirGPIO5D34reserve
E0 dirGPIO7D28
E0 stepGPIO8D26
Z maxGPIO11D19
Z minGPIO25D18
Y maxGPIO9D15
Y minGPIO10D14
X maxGPIO24D2
X minGPIO23D3
heater bedGPIO22D8
heater 2GPIO27D9uses for fan
heater 1GPIO18D10
ser 1GPIO17D11reserve
ser 2GPIO15D6reserve
ser 3GPIO4D5reserve
ser 4GPIO14D4reserve
I2C SCLGPIO3-to ads111x
I2C SDAGPIO2-to ads111x
ads1115 ch0-A15heater 2 - nc
ads1115 ch1-A14bed sensor
ads1115 ch2-A13extruder sensor
ads1115 ch3--

To test this, I decided to just hook up a motor to the x-axis. I wired my stepper motor to the driver board and the driver board to the pi as shown below.

 

image

 

To run the PyCNC file, you navigate into the PyCNC directory and run "sudo ./pycnc" and then you can enter gcode manually one line at a time. If you run "sudo ./pycnc filename" it will execute a whole file of gcode (where filename is the name of your file). When I tried running a simple command to move the x-axis, it didn't work and I double checked that my wires were connected correctly. 1A and 1B are for one coil and 2A and 2B connect to the other coil. So I changed my wires to be correct.

 

image

 

It still did not work. After reading some comments on the github page I played around with changing the stepper pulse length. This is done in the cnc folder in the file called config.py. When I changed the stepper pulse length to 10us it moved! The next thing to do will be to fine tune the dimensions such as how many steps per millimeter on each axis (that information is stored in the config file) and I'll have to test a whole file.

  • Sign in to reply
  • DAB
    DAB over 7 years ago

    Nice update.

     

    DAB

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • More
    • Cancel
element14 Community

element14 is the first online community specifically for engineers. Connect with your peers and get expert answers to your questions.

  • Members
  • Learn
  • Technologies
  • Challenges & Projects
  • Products
  • Store
  • About Us
  • Feedback & Support
  • FAQs
  • Terms of Use
  • Privacy Policy
  • Legal and Copyright Notices
  • Sitemap
  • Cookies

An Avnet Company © 2025 Premier Farnell Limited. All Rights Reserved.

Premier Farnell Ltd, registered in England and Wales (no 00876412), registered office: Farnell House, Forge Lane, Leeds LS12 2NE.

ICP 备案号 10220084.

Follow element14

  • X
  • Facebook
  • linkedin
  • YouTube