1. Do not share user accounts! Any account that is shared by another person will be blocked and closed. This means: we will close not only the account that is shared, but also the main account of the user who uses another person's account. We have the ability to detect account sharing, so please do not try to cheat the system. This action will take place on 04/18/2023. Read all forum rules.
    Dismiss Notice
  2. For downloading SimTools plugins you need a Download Package. Get it with virtual coins that you receive for forum activity or Buy Download Package - We have a zero Spam tolerance so read our forum rules first.

    Buy Now a Download Plan!
  3. Do not try to cheat our system and do not post an unnecessary amount of useless posts only to earn credits here. We have a zero spam tolerance policy and this will cause a ban of your user account. Otherwise we wish you a pleasant stay here! Read the forum rules
  4. We have a few rules which you need to read and accept before posting anything here! Following these rules will keep the forum clean and your stay pleasant. Do not follow these rules can lead to permanent exclusion from this website: Read the forum rules.
    Are you a company? Read our company rules

Tutorial Another ButtonBox (Arduino UNO)

Discussion in 'DIY peripherals' started by vthinsel, Apr 25, 2016.

  1. vthinsel

    vthinsel Well-Known Member

    Joined:
    Feb 20, 2015
    Messages:
    439
    Location:
    FRANCE
    Balance:
    6,010Coins
    Ratings:
    +564 / 2 / -0
    My Motion Simulator:
    Arduino, 4DOF
    Hello,

    I wanted to share with you my USB ButtonBox, which I made with Project Cars on PS4 in mind.
    [​IMG]
    The design is quite simple and requires few things:
    - An arduino UNO : be careful, as it must have the small Serial/USB AVR located on the top left part (with USB connector on the left). This is an Atmel Atmega16U2 or Atmega8U2 . Some clones do not have it.
    - Two I2C I/O extender to make a 8x8 matrix in order to have upto 64 buttons :) I'm using some cheap PCF8574
    - Push buttons, start button (all NO ,normally open)

    Key Concepts:

    • Turn the arduino to a keyboard : The ATmega16U2 chip (top left) on the Arduino board acts as a USB/serial interface allowing programming through USB. It runs a software like any AVR implemeting serial/USB conversion that can be updated through a special usb protocol called DFU (Device Firmware Update). Some other software will allow you to turn the ATmega16U2 into a USB HID (keyboard, mouse, or any Human Interface Device). Be careful: when USB HID code is uploaded on the ATmega16U2, programming the arduino becomes impossible unless you revert to the original ATmega16U2 code (or program using an AVRISP programmer) So program the arduino first, then the ATmega16U2 using procedure describded here and the firmware Arduino-keyboard-0.3.hex attached to this post.
    • Button matrix : the arduino has only a few pins available. Not enough to make a decent button box upto 64 buttons. Using an Arduino Mega would be overkill, so we need to be smart and use a matrix. A button is not attached to one pin but to two pins organised in row / column. To detect a keypress, we need to find the col/row making the code a bit longer than just checking the pin low/high state. We are using 2 I2C PCF8574 daisychained in order to make a 8x8matrix. Each PCF8574 is assigned a different I2C address resulting in two slave address. Only 4 wires (SDA/SCL/+5V/GND) are used on the arduino. Easy part :p

    The difficult part : soldering. Once the buttons are mounted, you need to implement the row/col matrix. To make things easier, I decided to have 8 columns to make wiring more straightforward. I started by wiring rows using unprotected wire, then I moved onto column soldering without making shortcuts by using some stripped insulant. Note the A0 jumper on rows .
    [​IMG]
    The code also implements a keypress repeat : leave a button pressed for 400ms and it will be repeated quickly every 30 millisec

    Steps:
    - I glued a millimeter paper onto 3mm plywood and made some holes with a dremel. Then drill the proper pushbutton diameter and mount all buttons (apply a 3M carbon look before if you need). Align all buttons connectors to make soldering more easy.
    [​IMG]
    - Implement the matrix as describded earlier
    - Upload arduino sketch
    - Program ATmega16U2
    - Done

    Maybe I'll rework the sketch to avoid using I2C extenders, as you may not neet 64 buttons, like in my example (8 col / 4 rows)

    Works great with PCars on PS4, but not with Dirt Rally as it doesn't support (yet.. I hope) additionals USB keyboards

    Enjoy !

    Attached Files:

    • Like Like x 4
    • Winner Winner x 2
    • Useful Useful x 1
    • Creative Creative x 1
  2. TFOU57

    TFOU57 Member

    Joined:
    Mar 30, 2009
    Messages:
    164
    Location:
    Thionville - France
    Balance:
    5,723Coins
    Ratings:
    +16 / 1 / -0
    My Motion Simulator:
    2DOF, AC motor, Arduino
  3. vthinsel

    vthinsel Well-Known Member

    Joined:
    Feb 20, 2015
    Messages:
    439
    Location:
    FRANCE
    Balance:
    6,010Coins
    Ratings:
    +564 / 2 / -0
    My Motion Simulator:
    Arduino, 4DOF
    Yes, but the approach is different here. I use an arduino uno and I2C extenders which I had in my stock :p
    Consoles are not very friendly with encoders and switches; only push buttons here.
    As mentionned in the post you refer to, the most obvious choice is to go for teensy. TIMTOWTDI
    • Agree Agree x 1
  4. RacingMat

    RacingMat Well-Known Member Gold Contributor

    Joined:
    Feb 22, 2013
    Messages:
    2,236
    Location:
    Marseille - FRANCE
    Balance:
    20,964Coins
    Ratings:
    +2,089 / 21 / -2
    My Motion Simulator:
    2DOF, DC motor, Arduino
  5. vthinsel

    vthinsel Well-Known Member

    Joined:
    Feb 20, 2015
    Messages:
    439
    Location:
    FRANCE
    Balance:
    6,010Coins
    Ratings:
    +564 / 2 / -0
    My Motion Simulator:
    Arduino, 4DOF
    Indeed. Excellent value/money. Got mine for ali. Used them to drive a LCD through I2C also: 2 wires instead of 8. Works very nicely.
  6. TOPMO3

    TOPMO3 Active Member

    Joined:
    May 12, 2015
    Messages:
    133
    Location:
    Russia
    Balance:
    893Coins
    Ratings:
    +201 / 0 / -0
    Great work, @vthinsel ! :thumbs

    just my 2 c
    if you use ON-OFF-ON spring return switches (ie those not locking but returning to central position after releasing them) you can wire them as usual buttons in your matrix, because technically they are 2 push buttons in one case
    Almost the same about encoders. Of course you need to process them differently in your sketch, but from PC point of view they also act as buttons - one for CCW and other for CW rotation
    • Like Like x 2
  7. vthinsel

    vthinsel Well-Known Member

    Joined:
    Feb 20, 2015
    Messages:
    439
    Location:
    FRANCE
    Balance:
    6,010Coins
    Ratings:
    +564 / 2 / -0
    My Motion Simulator:
    Arduino, 4DOF
    Thanks. You make me realize this is what I should have done for some of the switches : 1 switch instead of 2 buttons to move seat position front/back for example. Even no code change in that case.
    • Agree Agree x 1
    • Friendly Friendly x 1
  8. HecticZA

    HecticZA New Member

    Joined:
    Oct 20, 2017
    Messages:
    3
    Location:
    South Africa
    Balance:
    189Coins
    Ratings:
    +0 / 0 / -0
    I'm very interested in this project for the same reason you did it (PS4/ PCars 2)
    I have no knowledge regarding Arduino . I yesterday bough a pro mini, Nano and Uno because I did not know what I needed.
    I today ordered some On Off On momentary toggle switches and some momentary push button switches, but it will take a week to arrive .

    Would you mind if I contact you for some help regarding this project or should I rather post questions in this thread?
  9. HecticZA

    HecticZA New Member

    Joined:
    Oct 20, 2017
    Messages:
    3
    Location:
    South Africa
    Balance:
    189Coins
    Ratings:
    +0 / 0 / -0
    I see now that this is an old post.
    :(
  10. vthinsel

    vthinsel Well-Known Member

    Joined:
    Feb 20, 2015
    Messages:
    439
    Location:
    FRANCE
    Balance:
    6,010Coins
    Ratings:
    +564 / 2 / -0
    My Motion Simulator:
    Arduino, 4DOF
    So what ? :p
    The older a post gets, the better it becomes, like good wine .... and I'm not aware of expiry date on threads ;)
    You also need to order I2C extender as shown in the picture. Then time and patience will become your friend. This project is not the simplest to start with, but you can give it a chance.
    Good luck !
    • Like Like x 1
  11. HecticZA

    HecticZA New Member

    Joined:
    Oct 20, 2017
    Messages:
    3
    Location:
    South Africa
    Balance:
    189Coins
    Ratings:
    +0 / 0 / -0
    LoL, no I thought that the OP would't be around any more to take questions or give advice.

    So glad to see you are still active.

    I will see if I can find the extender. and also read a bit more about the process.
  12. Andi4x4

    Andi4x4 New Member

    Joined:
    Apr 25, 2018
    Messages:
    7
    Location:
    isle of man
    Balance:
    211Coins
    Ratings:
    +1 / 0 / -0
    Hi Guys,

    Sorry to resurrect this thread, but, I'm just about to embark on this project and need to know if I need to install libraries for the I2C extenders or will they work directly from the sketch in the download file.

    Thanks guys !
  13. Andi4x4

    Andi4x4 New Member

    Joined:
    Apr 25, 2018
    Messages:
    7
    Location:
    isle of man
    Balance:
    211Coins
    Ratings:
    +1 / 0 / -0
    Hi Guys,

    Sorry to resurrect this thread, but, I'm just about to embark on this project and need to know if I need to install libraries for the extenders or will they work directly from the sketch ?

    Thanks guys !
  14. Andi4x4

    Andi4x4 New Member

    Joined:
    Apr 25, 2018
    Messages:
    7
    Location:
    isle of man
    Balance:
    211Coins
    Ratings:
    +1 / 0 / -0
    Sorry for the double post - first one didn't appear to show til I posted the second !
  15. vthinsel

    vthinsel Well-Known Member

    Joined:
    Feb 20, 2015
    Messages:
    439
    Location:
    FRANCE
    Balance:
    6,010Coins
    Ratings:
    +564 / 2 / -0
    My Motion Simulator:
    Arduino, 4DOF
    Check the sketch for dependencies, or try compiling it :):

    #include <Wire.h>
    #include <pcf8574.h>
  16. Andi4x4

    Andi4x4 New Member

    Joined:
    Apr 25, 2018
    Messages:
    7
    Location:
    isle of man
    Balance:
    211Coins
    Ratings:
    +1 / 0 / -0
    Hi vthinsel,

    I haven't tried uploading the sketch yet, but, those two lines of code are present in the sketch. Just wondered if there was anything else I needed to include as I had to install a l2C library to use a LCD on another project.

    Will give it a go as it is to start with. Will it work as a keyboard before I flash the ATmega for testing purposes ?
  17. Andi4x4

    Andi4x4 New Member

    Joined:
    Apr 25, 2018
    Messages:
    7
    Location:
    isle of man
    Balance:
    211Coins
    Ratings:
    +1 / 0 / -0
    Ok, uploaded sketch to Arduino UnoR3 and got a "missing library pcf8574" so downloaded one from internet.

    Now getting the following error message when I try to verify/upload -

    (sorry guys, I am a complete novice with Arduino and code so any help gratefully received ! )


    Code:
    Arduino: 1.8.5 (Windows 10), Board: "Arduino/Genuino Uno"
    
    C:\Users\laptop\AppData\Local\Temp\arduino_modified_sketch_135813\PS4_ButtonBox.ino: In function 'void setup()':
    
    PS4_ButtonBox:172: error: 'class PCF8574' has no member named 'write8'
    
       PCF_COL.write8(0xff);  //Pull high
    
               ^
    
    PS4_ButtonBox:173: error: 'class PCF8574' has no member named 'write8'
    
       PCF_ROW.write8(0xff);  //Pull high
    
               ^
    
    C:\Users\laptop\AppData\Local\Temp\arduino_modified_sketch_135813\PS4_ButtonBox.ino: In function 'void loop()':
    
    PS4_ButtonBox:186: error: 'class PCF8574' has no member named 'write8'
    
         PCF_COL.write8(~hex_data[r]);
    
                 ^
    
    PS4_ButtonBox:187: error: 'class PCF8574' has no member named 'read8'
    
         uint8_t valueCOL = ~PCF_COL.read8();
    
                                     ^
    
    PS4_ButtonBox:188: error: 'class PCF8574' has no member named 'read8'
    
         uint8_t valueROW = ~PCF_ROW.read8();
    
                                     ^
    
    exit status 1
    'class PCF8574' has no member named 'write8'
    
    This report would have more information with
    "Show verbose output during compilation"
    option enabled in File -> Preferences.
    
  18. Andi4x4

    Andi4x4 New Member

    Joined:
    Apr 25, 2018
    Messages:
    7
    Location:
    isle of man
    Balance:
    211Coins
    Ratings:
    +1 / 0 / -0
    Finally !

    Got it sorted - the PCF8574 library I had was not suitable, so, a couple of hours spent trawling the internet eventually yielded one that did, then issues with the Atmel Flip flash software slowed me down another couple of hours.

    Now, however, just over 4 hours later, it is all working !

    Yay !

    Button box construction is well under way - box is drilled, carbon effect vinyl applied, switches fitted - just gotta solder it all up !

    Will take some pics and start a separate thread for that in the appropriate place !

    But now - well deserved Bed Time !
    • Winner Winner x 1
  19. marc burns

    marc burns New Member

    Joined:
    Apr 19, 2017
    Messages:
    2
    Location:
    illinois
    Balance:
    233Coins
    Ratings:
    +0 / 0 / -0
    Do i still need 10k resistors for this project?????
  20. vthinsel

    vthinsel Well-Known Member

    Joined:
    Feb 20, 2015
    Messages:
    439
    Location:
    FRANCE
    Balance:
    6,010Coins
    Ratings:
    +564 / 2 / -0
    My Motion Simulator:
    Arduino, 4DOF
    There are no resistors in this design..