Intro: Build an Arduino Into a Nissan Qashqai to Automate Wing Mirror Folding or Anything Else
The Nissan Qashqai J10 has a few little annoying things about the controls that could easily be better. One of them is having to remember to push the mirrors open/close switch before taking the key out of the ignition. Another is the little configurability of the BCM (body control module) and the Nissan Connect head unit. There are a few other things but most importantly it doesn't play the Imperial March when you start the engine, like my quadcopters do! Something needed to done.
The former has reportedly been solved in newer J11 models (2015+?) but I think for the 2014 J11 Qashqai you still need to buy a kit. There are separate kits for the J11 and the J10 models (2008 - 2013 or so) like the Nissan offical kit (don't know the price), the £70 AcesDVD kit from some folks at qashqaiforums.co.uk and a few newer options. One has even popped up on aliexpress for just €17 but is no longer available. These kits tend to connect to just 8 wires in the car that you need to locate and they'll automatically fold the mirrors in when you lock the car and fold out when you unlock it, so that's really useful but still doesn't give you much flexibility.
So having around some $1 Arduino clones and a few drawers of MOSFETs, transitors, a speaker and other components at the local hackerspace and knowing little about analog electronics I set out to replicate what those kits do but with the flexibilty to change the logic by reprogramming the Arduino board over USB at any time.What can be complicated about connecting an Arduino and a few MOSFETs, right? It turns out there are a lot of quirks when driving a motor with MOSFETs or transitors which meant re-designing the connections a few times, adding a $1.50 H-Bridge from aliexpress and a bunch of resistors, but it works and I've learnt a few things. Here's how you can do the same thing if you prefer it over buying a ready made kit for somewhere between €17 and €90. It takes probably a day to figure everything out, solder what needs to be soldered, program and do the wiring.
Once you have the arduino with power supply problems resolved, doing nothing 99% of the time, and know how to access the wiring in the car, you can add a lot of other mechanisms by connecting other signal wires to the board. So far I made mine play the Star Wars imperial March when I start the car, either using the tiny speaker, or actually using the DC motors in the powered mirrors which, driven with the Arduino's PWM signals are just as good as drone motors for playing sounds. The arduino will fold/unfold the mirrors with a 1 second delay on car lock events (to distribute the load) and also allow you to manually fold/unfold mirrors for 15 seconds after disconnecting the key since the arduino shuts itself down after 15 seconds of invactivity (all configurable). It also now controls the power to the windows, so I can close them for 15 seconds after taking the key out.
Note that if it isn't for the fun it's not worth doing all this, honestly just buy the kit and save time.
Step 1: Overview
So my Qashqai is the 2013 J10 model, left-hand drive (for right-hand side traffic), non-Intelligent Key and non-Superlock version, but this should apply to all J10 models equipped with powered wing mirrors, perhaps to J11 and possibly other models. The wiring is slightly different in each version of the J10, all in all you have 8 combinations (LHD/RHD, iKey/no iKey, Superlock/no Superlock) with the differences documented in the service manuals which I'll link, J11 is also well documented.
What you need:
- a PC with the Arduino IDE installed,
- two Arduino 5V boards or clones thereof. I use aliexpress 5V 16MHz Pro Mini clones like these ones. One is the one for the car and you need a second one, or an actual ISP programmer, to reprogram the bootloader on the first one. You could use any other development board too but they need to be of the dumb type (like an Arduino, not a Single Board Computer type) to make sure they boot up quickly. You could add an SBC alongside the Arduino though.
- an H-Bridge like the popular L298n chip unless you want to build your own with 6 to 8 MOSFETs or transistors and a few other components. I use these 2-channel aliexpress L289n breakout boards with everything included.
- four diodes of any type that support up to about 15V (almost any through-hole diode).
- a set of resistors of 100kΩ, 47kΩ, 4.7kΩ or so, I use through-hole resistors found at my local hackerspace.
- a P-channel power MOSFET that can sustain 1A or 2A (alternatively a PNP transistor), I use the IRF9540n. If you also want it to power the windows to go through it, aim for at least 5A.
- a small N-channel MOSFET (alternatively an NPN transistor), I use the 2n7000 but a bigger one like the IRF540 or RFP50N06 also works.
- optionally a speaker and a 100Ω resistor.
- cables, some of about 18 AWG for the car wiring (I use 18 or less AWG silicon cables from aliexpress) and some thin wire for connecting the components together, optionally a solder or solderless breadboard to mount everything on and pin headers.
- jumper cables, a multimeter, pliers, soldering iron and a flat screwdriver to remove the car covers.
Step 2: Program the Bootloader
The board number one will go in the car. Board two will only be needed to flash the bootloader on the Arduino number one, once. This is because AVR-based Arduinos tend to ship with the older bootloader which has a 500ms or 1s delay built-in before starting your programs, to allow time for a programmer to signal it. The new default bootloader is optiboot, which has a mechanism that lets it run programs immediately on power-on.
In the car, the Arduino is going to be powered-up by one of the following three signals: ACC power on, lock or unlock. The latter two signals are short 12V pulses that only wake us up for a moment, from there the Arduino will need to use one of its digital pins to signals it wants to keep receiving power. We need to reflash the target Arduino with optiboot so that it can do this quickly enough, before the pules ends and we lose power. (You could work around it by adding a big capacitor but meh)
Connect board two to the PC -- if it doesn't have a USB port, like the Pro Mini clones I use, solder 5 male pins on the GND, VCC, RXD, TXD, DTS side of the board and connect through a USB-to-Serial adapter. Then open the Arduino IDE, from File/Examples load Arduino ISP and uncomment this line:
(if you Arduino IDE is new enough to have this, otherwise you don't need to uncomment anything). In the Tools/Board menu you will need to select one of Arduino Pro or Pro Mini, Arduino/Genuino Duemilanove or Arduino/Genuino Uno depending on the baud rate configured in the bootloader that shipped on your boards. Try them until you can upload the sketch to the board. Disconnect the board.
Solder pins, male or female, depending on what jumper cables you've got available, on the digital pins 10, 11, 12, 13 on both boards (you could use male jumper cables without any headers, but meh...), and RST, VCC and GND on board one. Actually on that board you'll probably need headers all pins on that side of the board so might as well solder them directly. Then connect pins 11, 12 and 13, VCC and GND of both boards together and pin 10 of board two to RST of board one. You may want to use the alternative VCC and GND pins on board two so that you can keep the USB-to-Serial adapter attached too.
Finally connect board two to the computer, download the latest optiboot release optiboot.zip from https://github.com/Optiboot/optiboot/releases and follow the Installing using the Arduino IDEinstructions on the wiki. Alternatively if you're running Linux and have avrdude installed, just unzip optiboot.zip and run the following commands:
avrdude -p m328p -P /dev/ttyUSB0 -c avrisp -b 19200 -u -U lfuse:w:0xdf:m -U hfuse:w:0xdc:m -U efuse:w:0xfd:m -v -v
avrdude -p m328p -P /dev/ttyUSB0 -c avrisp -b 19200 -u -U flash:w:Optiboot/bootloaders/optiboot/optiboot_atmega328.hex:i -v -v
The first command sets the AVR fuses so as to speed up sketch startup even more at the expense of clock stability. Adjust the /dev/ttyUSB0 path according to what your USB-to-Serial adapter shows up as.
Step 3: Program the Arduino Sketch
Now you can connect the Arduino board 1 directly to the PC, open this sketch in the Arduino IDE and compile and upload the sketch to the board. If you're using the 16MHz Pro Mini like I do, with the binary optiboot release, you'll need to first select Arduino/Genuino Uno from Tools/Boards.
You can later come back and change any of the pin mappings and options in the code. If you later make any fixes or improvements to the code remember to contribute them back in a github pull request some time.
Step 4: Build Your Electronic Board
Finally you'll need to connect all the components together and there are many ways to do that. You'll need to end up with schematic as shown in the picture (or here). The easiest way seems to be to make all the connections on a breadboard and have pin header row to connect the Arduino board, another 2 pin headers for the speaker positive and negative, 2, 3 or 4 pin headers to connect to the H-Bridge breakout depending on its type, and thicker cables to eventually make the 12V connections to the car wires and the PWR and GND cables to the H-Bridge. My board came out pretty terrible but it works, you can see it in the images above.
Some notes about the schematics:
- For simplicity I decided to put all the through-hole components and the pin headers and on one side of the breadboard PCB, and the actual connections between them, with wires or solder blobs, on the other.
- The board layout, if you even want to use a PCB, doesn't need to be anything like the schematics layout.
- My board has wires for GND, ACC, SWITCH-, MIRROR+ and MIRROR- of about 8 cm, they will all connect to the M7 connector in the car which is right below the dashboard. My BAT+, LOCK+ and LOCK- wires are longer because they connect elsewhere.
- The resistors R1 to R8 form the voltage dividers for the 12V input signals to be read by the Arduino's digital pins. The relation between the 47k and 100k resistors is about 2:1 which, for a 5V Arduino (with about 3V to 5.5V input tolerance for high level) means that he voltages from the car can range from about 9.5V to about 17V. That should be enough for everything to work even with a heavily discharged battery in the car up to full and even up to the 4-cell Lithium Polymer battery like those used in drones (sometimes also used to start cars with a discharged battery if that's all you have). You can use different resistor values but they also need to be close to or higher than the Arduino's built-in pull-up resistor on the digital pins, because we the sketch uses the pull-up to detect HIGH, LOW and floating states on the pin. That's also the reason a typical I2C level shifter can't be used for the level translation. The I2C level shifter include permanent pull-up resistors and would complicate things a lot. Our voltage dividers work as pull-downs.
- The two mosfets allow the Arduino to fully power down when it decides it no longer needs to do anything to make absolutely sure the car battery is not being drained if you leave the car for a longer period of time. The P-channel MOSFET has to carry all the current to the H-Bridge, the mirrors motors and other potential motors so it need to tolerate about 1A at the minimum, and much more if you're going to also power the windows through it.
- I have used an additional 4.7K resistor at the N-channel mosfet gate to protect it, things still work without that resistor but I burnt a few 2n7000 mosfets while testing and wanted to have all the possible problems covered.
- If you use a PNP transistor (such as the TIP135) in place of the P-channel MOSFET, you can skip the R10 resistor because the gate will have less capacitance.
- If you also use an NPN transistor (such as the 2N2222A) in place of the N-channel MOSFET then you can also skip the R9 resistor.
- If you think it's a good idea to build your own H-Bridge have a look at this page, it lists a number of H-Bridge designs and some pitfalls to be aware of.
- R2 and R6 could possibly be gotten rid off as well if you made the Arduino sketch smart enough to detect the unlock signal from just he lock wire.
- The H-Bridge current sense signal (SenseA) is optional and my current sketch doesn't even make use of it. The aliexpress L298n breakout comes without the current sensing resistors shown in the reference schematics in its datasheet, but they can be added easily (require cutting a trace).
- If you change the Arduino's pin mapping, the only considerations are: LOCK+ is connected to an analog-capable pin to enable sensing both the lock and unlock signals from that pin in the future. The current sense signals is also an analog-capable pin. The H-Bridge's ENA signal and the speaker are both connected to PWM-capable pins to enable generating PWM on them but again that isn't currently used.
- If you use a piezo speaker you shouldn't need resistors at the speaker's positve pin. For other types of speakers you'll probably need a 100Ω resistor between Arduino's pin 10 and the speaker, which is not in the schematic.
Step 5: Find the Lock Signals in the Car
This one is a little tricky and where I've seen a few of the people who bought the ready-made kits fail during installation, according to buyer comments. The service manuals are helpful to locate the right wires but only to a degree because these manuals are made for diagnostics (if this, do this..) rather than documentation. I copied some pages from one of the versions of the manuals that you can google, and I added some notes over them.
Have a look at the diagram on page 72 (for LHD) or 89 (for RHD) for the connector names on the Main Harness. I placed my arduino under the dashboard close the mirror controls so I wanted to connect to wires in the Main Harness.
For most signals we can use the cables going to the M7 connector which actually plugs right into the mirror controls assembly. However the battery positive and the lock positive (or unlock negative) and lock negative (or unlock positive) wires are not there. There's actually more than one unlock wire (lock negative) coming from the Body Control Module because doors can be unlocked independently. We can use any of the unlock signals for our purpose. However the doors can only be locked simultaneously so there's just one lock signal (unlock negative).
In the left-hand drive version the single lock positive signal is routed to the rear doors through the right half of the car so we can't use the M13 connector, which would have been convenient, because it only has the unlock signal. In the right-hand drive cars you can use the wires going to the M11 connector which has both the signals we need and is easily accessible. In the LHD version I spliced the cables that go to the M19 connector which has the lock and unlock wires for the front driver door (M19 plugs into D2 in the Door Harness on page 82). The M19 is quite inaccessible but the cables that go to it exit a big plastic tube together with the cables for M18, M77, M78, M13 and M14 so they're easy to locate. The M11 connector cables for the right-hand drive version exit a tube that's in the same location but on the right side.
To get there you first need to remove what the manual calls the "front kicking plate" which is the plastic part of the floor right by the driver door. (I believe that's number 4 in the diagram on page 14 of this part of the service manual). You can force it up with a screwdriver which should make its plastic tabs detach from the floor and you should then see all the cables and connectors in the front part. Next is the "dash side finisher", which is a plastic cover starting at the floor, to the side of the pedals (number 1). It's front end has a plastic screw going through it (number 12 in the diagram) with a plastic nut that needs to be removed and then the whole thing can be detached by pulling by hands hopefully without the plastic tabs breaking. Optionally you may want to remove the "lower instrument panel", page 14 here.
The connectors that you'll now see connect the Main Harness to the Body Harness (M13, M14), the Engine Room Harness (M77, M78) and the Door Harness (not visible, M18, M19).
Page 630 of this part of the service manual shows the layout of the M19 connector for the "WITHOUT I-KEY & SUPERLOCK" configuration, you can look up the pinouts for each configuration in the index but the colour cables should be mostly the same. In my case it says (on page 630) pin 2 "GR" for grey and pin 3 "SB" for sky-blue. Page 626 shows how those are wired all the way from the BCM to the "front door lock actuator (driver-side)", but basically the colours are all we need to know. In my case grey is unlock and sky-blue is lock.
So when you locate about 6 bunches of cables going out from that wide corrugated tube just to the side of fuel lid lever, 4 groups of cables will go to the connectors further down, while 2 will go somewhere to the left. Of these two I noticed one has thicker cables, this is the one that goes to the M19 connector. Locate a grey, a sky-blue and a pink cable. Pink is the battery positive. In my case there are two sky-blue cables with some sort of hand-made dot markings and the one we need is the slightly thinner one of the two. I ended up making small cuts with a cutter and checked which one showed a short positive pulse on the multimeter when locking the car. Then I cut all three cables with pliers, made the connections again with screw terminals and then added three extension cables (2x white, 1x red) of about 40cm to route the signals to where my Arduino was going to be (near the mirror controls). Nothing should happen but you might want to first cut one cable, lock its lose ends in the screw terminals, only then cut the next one to avoid shorting something.Note: you can temporarily disconnect most things to get cables out of your way, but if you disconnect the M77/M78 connectors the whole dashboard will lose battery power and your clock will reset and your Nissan Connect will be asking you for the security code.
Note: some of these connectors have other interesting signals, like for example M13 has wires from the door opened sensors so if you wanted to do any kind of automation on the Arduino that needs to know whether any door is opened you could take the chance to connect also splice the respective cables to wire them to the Arduino.
Note: for the purpose of this instructable you could also mount the Arduino inside the door and you'd have access to all the signals in one place.
Step 6: Optional: Windows Power
While you're there you can also prepare a 4th cable that will power the window controls and motors from our Arduino controlled MOSFET instead of from the BCM wire that only supplies 12V when the key is in the ON position. That'll let you control the windows for those 15 seconds that we've programmed the Arduino to stay powered after disconnecting the key. However you'll need an accordingly hefty P-channel MOSFET and wiring. I have yet to investigate if my wiring is not stressing the fuses or the BCM a little too much but I have not yet blown any fuses.
So to do that you'll need to locate the two "blue" (not "sky-blue") cables going to the M19 connector. The one where we'll be injecting power is the thicker one of the two, pin number 8 at M19. Both are normally shorted together though so there's no way to find out which is which with just a multimeter until you cut one of them. Just cut the slightly thicker one. Now we won't need the upper half of it (the one that normally supplies power from the BCM through the Main Harness), so just wrap that end in electrical tape. Use a screw terminal to extend the other half (the one that goes to M19) similarly to the other three extension cables we've prepared.
I then wrapped the whole thing including the screw terminal strip with lots of electrical tape, also wrapped my four extension wires together and routed them under the dash covers. With this done you can mount "kicking plate" and "dash side finisher" back in their places.
Note: this fourth wire is optional but you can do this even if you plan to find a big enough MOSFET later so that you don't have to fiddle with the cable harnesses again. In the meantime you can connect this fourth cable directly to the ACC power in the next step.
Step 7: Connect the Contraption to the Mirror Control Wires
Now that you've got all the 8 cables near the mirror controls you can take another screw terminal strip and connect everything together. You'll notice the mirror controls are mounted on a roughly rectangular plate that you can force out with a flat screwdriver. On the inside it'll have three sockets, the biggest one is where the M7 connector from the Main Harness plugs into. See above the pinout of the M7 connector with the notes I added. You'll basically need to cut the wires for pins 1 (GND, black), 3 (ACC, red), 8 (MIRROR+, orange) and 9 (MIRROR-, blue).
These are the connections you'll have to make:
- The LOCK+ (lock) and LOCK- (unlock) and BAT+ (battery positive) extension wires from M19 from the previous step to our circuit board.
- The GND cable you cut in two needs to be joined back together with a screw terminal and spliced to also connect to our circuit.
- The MIRROR+ and MIRROR- signals are not spliced. The halves that go to the Main Harness need to be connected to the H-Bridge, while the half of the MIRROR- signal from the M7 connector needs to connect to the SWITCH- signal that goes to the Arduino through the voltage divider. The other cable isn't needed but lock it into the screw terminal so it's not lose.
- The ACC wire from the harness connects to our board while the PWR output from our board connects to where ACC was connected at the M7 connector. You can use two screw terminals to connect the two halves of the original cable to the ACC and PWR wires from our new circuit.
Connect the Arduino and the speaker to the rest of the circuit and apply electrical tape everywhere, or you can design a pretty 3D-printed case to hold everything together. I opted for the electrical tape everywhere method myself. I just left the USB-to-Serial adapter connected to the Arduino, tested that the Arduino reacts to all the right events by making sounds with the speaker, then pushed the mess through the opening for the mirror controls panel, put that panel back in and only left the USB connector exposed for further changes in the sketch.
Step 8: Test
If you've gotten most of the cables right, the only problem left will be figuring out the lock/unlock signals polarity, the mirror motor wires polarity and the switch signal polarity. With my sketch as is you should at least hear the Imperial March tune play when turning the key to the ACC position, and the mirrors should either fold in or out. If they fold in instead of out, just switch the PIN_HBRIDGE_DIR1 and PIN_HBRIDGE_DIR2 pin numbers in the sketch, and re-upload to the board. Next, if the manual mirror switch is working the wrong way, uncomment the
line. Finally try locking and unlocking the car, if the mirrors are moving the other direction then switch the PIN_LOCK1_IN and PIN_LOCK2_IN pin numbers in the sketch.
Step 9: What Else Can Be Done
- Close windows and the roof on car lock and possibly restore to last position on unlock. This should also work with H-Bridges but I'm not sure there will be enough IOs left on the Arduino for all the wires. You'd need current sensing to be able to sense how long the motors had ran to be able to restore to the same position later. Simply closing the windows on lock is easier because you just need one output pin and one half of an H-Bridge with additional diodes or MOSFETs to avoid a short if someone had been actuating the manual window controls simultaneously. Wiring all this seems easy for the passenger and rear windows because all of this goes through the D8/B8 connector, however the driver window is more tricky.
- According to the forums it may be undesired to try to fold the mirrors in the winter if the mechanism is frozen. The Arduino has an NTC thermistor and can automatically decide that instead of the driver having to touch the mirror switch twice a year.
- Find out if the reverse gear signal to Nissan Connect is a single wire or an OBD2 signal. I'd love for Nissan Connect to keep showing the rear camera view for a few seconds after changing to a forward gear, and also to show the rear camera view when the car is rolling backwards without the reverse gear engaged. My main annoyance with this system.
- Add a Raspberry Pi or another SBC alongside the Arduino to process OBD2 signals as well as signals from the Arduino, do the logging and additional smarts.