Saturday 2 June 2012

Sanguinololu, LCD and rotary encoder with switch

Having seen the PanelMax LCD and Encoder for RAMPS, I wanted to get a similar setup working on Sanguinololu. the first step was to get an ATmega 1284P working and load Marlin firmware which supports this sort of setup with minimal changes.

Wiring and Testing

I intended to keep using the SDSL card reader (not much point in having a screen if you have to have you computer plugged in) which left very few pins unused on the microprocessor. Conveniently these are all together on the expansion header:

The image is grabbed from the .brd eagle file - easier to show than on an actual board. There are 9 I/O pins free, one of which is within the SDSL plug footprint but is not used by SDSL. Matching them up to the requirements for a standard 20x4 LCD with a HD47780 interface, and a rotary encoder (with a built in switch) gave;

For the LCD;
RS               "PWM"               Digital pin 4
ENABLE      "SDA"                Digital Pin 17
D4                "A1"                  Digital Pin 30
D5                "A2"                  Digital Pin 29
D6                "A3"                  Digital Pin 28
D7                "A4"                  Digital Pin 27
For the Encoder;
EN1              "RX1"               Digital Pin 10  (must be a hardware interrupt pin)
EN2              "TX1"                Digital Pin 11 (must be hardware interrupt pin)
SW1             "SCL"                Digital Pin 16 (Click switch)

Adds up to 9, just enough!

I connected the circuit up (quickly, I was impatient!) as shown in the Schematic. Note that the connection marked SCL on the Sanguinololu expansion header is plugged onto by the SDSL plug, but not used by the SDSL, I have connected onto this pin where it comes out of the plug as it is needed for the push switch in the encoder. The two variable resistors as 5K as that is what I had to hand, the one for the LCD brightness (R1) is set to around 1K, the one for the LCD contrast (R2) is set to around 4.5K Ohms.

After connecting it up I loaded  the Arduino 0023 IDE, and modified the "Hello world" example program from the LiquidCrystal library. The only modifications made were to the pin assignments;

    LiquidCrystal lcd(4, 17, 30, 29, 28, 27);

and define it as a 20x4 LCD

  lcd.begin(20, 4);

 So first step accomplished: the Sanguinololu talking to an LCD Screen.

Next I messed around with some of the examples shown on the Arduino rotary encoder page and ended up with a bit of a hybrid one that allows you to move the cursor around the screen with the encoder, with an arrow to show which way its going. Clicking the switch on the encoder resets it to the middle.


The code is available here (EncTest.pde), if you want to have a play with it.

Working with Marlin

Using the modifications on tommyc's blog as a starting point I checked and modified the Marlin Configuration.h, Configuration_adv.h and pins.h as shown below:

Line 34:
  #define MOTHERBOARD 62 
Line 190-205:
    //to disable EEPROM Serial.....
    // please keep turned on if you can.

  //LCD and SD support
  //#define ULTRA_LCD  //general lcd support, also 16x2
  //#define SDSUPPORT // Enable SD Card Support in Hardware Console

  #define ULTIPANEL
  #ifdef ULTIPANEL
    #define NEWPANEL  //enable this if you have a clicencoder panel
    #define SDSUPPORT
    #define ULTRA_LCD
    #define LCD_WIDTH 20
    #define LCD_HEIGHT 4

Line 163:
pins.h, making sure to be within the sanguinololu pin definition part of the file. For my pins.h this was lines 557 - 657. I added the following 34 lines, copied from the RAMPS section of the pins.h

  #ifdef ULTRA_LCD
    #ifdef NEWPANEL
      //we have no buzzer installed
      #define BEEPER -1
      //LCD Pins
      #define LCD_PINS_RS        4
      #define LCD_PINS_ENABLE    17
      #define LCD_PINS_D4        30
      #define LCD_PINS_D5        29
      #define LCD_PINS_D6        28
      #define LCD_PINS_D7        27

      //The encoder and click button
      #define BTN_EN1 10  //must be a hardware interrupt pin
      #define BTN_EN2 11 //must be hardware interrupt pin
      #define BTN_ENC 16  //the switch
      //not connected to a pin
      #define SDCARDDETECT -1
      //from the same bit in the RAMPS Newpanel define
      //encoder rotation values
      #define encrot0 0
      #define encrot1 2
      #define encrot2 3
      #define encrot3 1
      #define BLEN_C 2
      #define BLEN_B 1
      #define BLEN_A 0
    #endif //Newpanel
  #endif //Ultipanel

Once these changes had been made, Marlin compiled to be 80k+ so definitely too big to go on the 644P. After upload the Printer status display came up:

 And rotary encoder worked fine to navigate the menu system...

I selected a random GCODE file from the SD card and started printing it (or rather the Sanguinololu controller started processing the GCODE, its not connected to anything other than a 5k pot to simulate the hotend thermistor)

Once the printing starts you can use the encoder to "tune" the speed of the printer, its currently set to 200% below:

The pseudo print continued without issues. The next step will be to tidy up the wiring and get it connected up to a live printer!


  1. GLad to see this work going on. Let me know when you have an LCD working Sanguinololu and I"ll probably order it. Do you know if Marlin will work with Sanguinololu 1.1? That is what I have. I just ordered a 1284p from you guys in anticipation.

    1. I don't have a Sanguinololu 1.1 but Marlin should work. As for the LCD screen + click encoder, that will also work however the 1.1 has a slightly different expansion header. the different header will mean wiring the connector we intend supplying with our kits in a slightly different way.

  2. I have just finished the same project (Sanguinololu 1284P with Marlin 1) but I ended up using Lady Ada's LCD BackPack ( )to save and extra 3 pins (for the fan and buzzer). With time you could actually run the LCD on the same SPI interface as the SD and save 6 pins but I run out of patience.

    I also could not find the solution you got to with avrdude config file, so I went brute force and changed the resonator for a crystal and corresponding caps.

    I also found out that the D12 (PWM) pin on the header (as you can see in the picture and in the board)is actually connected to D4.

    1. Yeah I have been paying with I2C extensions for some time but nothing solid yet. I did not want to use an LCD backpack (played around with this one from hobbytronics because I felt (at the time) that if I went to I2C I wanted to have everything (click encoder, LCD, fans, buzzer etc) on the I2C bus so it was easily portable to RAMPS, Gen 7 etc.

      After pouring a huge amount of time into developing an atmega328p based I2C extender I got frustrated with integration at a software level. I want to offload the UI to the 328P and use GCode to send commands to the main board - its a project on the back burner right now but I am interested in a common, open, bus - based, extension to reprap electronics that would allow me to add bits to a standard board cheaply. That way when we all change to something like a Smoothieboard ( or start printing with 3 or more colours then the upgrade path should be easy.

      As far as the D4/D12 pin is concerned its labelling error on the board, I relied heavily on the Sanguinololu schematic which is labelled correctly.

  3. I actually left the hardware SPI for the SD reader and implemented a software SPI for the LCD (no need for MISO for the LCD). I tried to get both on the same bus but I am not that versed nor patient. The PINS.H relevant section looks like this:

    #define SDSS 26
    #define SDCARDDETECT 17

    #define LCD_PINS_DATA 28
    #define LCD_PINS_CLOCK 27
    #define LCD_PINS_LATCH 25

    #define BTN_EN1 10
    #define BTN_EN2 11
    #define BTN_ENC 16 //the click

    I have also encountered problems getting the fan to work on pin D29. It stops all Z movement when it goes high.

    By the way thank you very much for posting your implementation. It is really helpful, well presented and would have saved me days of trials getting the boards to communicate.

  4. is it possible to asign a Fan to the board aswell or are all free pins used by the LCD and SDCard

  5. The way I describe above uses all the free pins from the ATmega. Bluemetal's method (in previous comments) using an 12C LCD backpack leaves you with more free pins - enough to have a fan and a few spares.

  6. Thanks a lot!!! I spent many hours to understand compile problem. And now with your description its compiled well!!!
    Thank you!

  7. I was able to run a fan with this setup by modifying the board so that there is only one enable circuit. This frees up a pin for the fan.

    1. Good work - especially a good idea if you have a delta printer and there is no advantage to being able to enable the Z axis seperately.

  8. super, it was a great help for me too. My only trouble is that the "BTN_ENC 16 //the click" is still output, not input....however it is declared as input somewhere.

  9. can a spare pin be used to control a servo? i have the a Sanguinololu 1.3a with the panelolu 2 lcd that i purchased from you and i want to use the z bed auto level function, the new marlin firmware update, any help would be great i have power to the servo from the 5v and gnd on the pin headers of the panelolu 2.

    1. Hi

      Yes it should work using either the PWM or A3 pin on the Panelolu2 adapter board expansion header +5V and GND but i have not tried this. More info here:
      on what pins go where.

      You will need to look at the Servo implementation is more up to date Marlin and see how its done with RAMPS and then modify that to work with Sanguinololou

    2. Did anyone ever figure out using a servo on one of the I2C pins? - I'm also interested in using auto-levelling from a Panelolu2. preferably with Marlin as well...

  10. Hi there, am I right in saying that this has to be compiled as Sanguino 1284p , not as Sanginololu 1.2+ because trying to compile as Sanginololu 1.2+ just kicks up nothing but errors during the compile.

    1. Hey

      Yes you need to use the Sanguino extensions and have the chip setup for Sanguino 1284p.

      See the summary at the end of my first blog post:



  11. Sir, I have 1284P square IC not the rectangular IC. I wanted to display HELLO WORLD ON IT. I Followed your this post by replacing

    LiquidCrystal lcd(4, 17, 30, 29, 28, 27);

    lcd.begin(20, 4);

    Its not displaying. Please what configurations I hould use. I have sanguino made own board... Help ASAP

  12. Dear Sir,
    I trying to interface Reprap Full 128*64 with my 1284P/644P. I have connected the pins as per your sanguino. But its not displaying. Please tell me how can I connect.

    Reply here or to



Comments are now moderated to reduce spam, please excuse the additional time this will entail.

Note: only a member of this blog may post a comment.