Go Down

Topic: Arduino Nano (CH340G) based USB-TTL adapter (Read 292 times) previous topic - next topic

6v6gt

Sep 11, 2017, 04:53 pm Last Edit: Sep 11, 2017, 06:43 pm by 6v6gt Reason: added pictures
I've converted an Arduino Nano clone into USBTTL adapter to upload sketches and view the serial console for 5 volt tolerant devices which don't have a USB interface such as a barebones ATmega328P, an Arduino mini, Adafruit Huzzah ESP8266 etc. This is intended to be part of a larger programmer project but it also works as a stand alone system and could be useful for those who have a spare Nano but no USBTTL adapter.

This works with the standard Chinese Nano clone which has a CH340G such as is described here with a schematic diagram: http://actrl.cz/blog/index.php/2016/arduino-nano-ch340-schematics-and-details/

The idea is very simple. A hardware change is made to the Nano to enable a sketch to detect the falling edge of the DTR pin on the CH340G. This hardware change consists of a 0.1 uF capacitor between the DTR pin of the CH340G and pin A6 of the Nano. There is also an external 10K pullup resistor added on A6. The sketch runs on the Nano and detects the falling edge on the the DTR and propogates a 100uS low pulse to reset the target system as required.
Other harware changes are disabling the Rx/Tx Leds and bridging the two 1K resistors in the serial lines between the CH340G and the ATMega328P. These last changes were necessary to get a reasonably reliable upload to the Adafruit Huzzah (see below)

Connections

Code: [Select]

    Wiring:        Target System Pin
    Nano Pin       (must be 5Volt tolerant)
    --------       -----------------
    GND            GND
    5V             Vcc / 5V
    TX1            TX  ( via 1K resistor ) yes: Tx to Tx in this case
    RX0            RX  ( via 1K resistor ) yes: Rx to Rx in this case
    D2             RESET (if required on target)
 

Also, the Nano Reset pin has a 10uF (or so) capacitor to ground to prevent it [the Nano] from being reset during the time an upload is sent to the target system.

In the breadboard picture, the Nano is set up to load the blink sketch onto a ATMega328P target system.
I have also used it to upload sketches to an Adafruit Huzzah ESP8266 breakout board which is no mean feat because this is notoriously sensitive about which USBTTL device it will accept, which I guess is at least partly due to the very simple voltage level shifter on its Rx pin (a 1n4148 diode) which prevents the USBTTL device bringing the pin fully low and stops it pulling it high at all.









Sketch

Code: [Select]

/*
    USB TTL Serial adapter based on Arduino Nano CH340G clone

    Nano modified as follows:
    0.1 uF between CH340G DTR (pin 13) and Nano pin A6
    10K ohm pullup between Nano pin A6 and Vcc
    10uf-22uF capacitor between Nano Reset pin and ground to be present when connecting to target system.
    Tx and Rx Leds disabled by removing the correspondin resistors.
    The existing 1K resistors between CH340G TX/RX (Pins 2 & 3 ) and ATMEGA328P-AU RX/TX (pins 30 & 31) to be bridged
    
    See http://actrl.cz/blog/index.php/2016/arduino-nano-ch340-schematics-and-details/ for a Nano CH340G schematic,
    but note that there may be differences:
    1. The pullup resistor on the ATMEGA328P chip is given as 10K. On my samples, these are a very strong 1K.
    2. The regulator is shown as LM1117-5.0. On my samles it is an AMS1117.
    

  
    Wiring:        Target System Pin
    Nano Pin       (must be 5Volt tolerant)
    --------       -----------------
    GND            GND
    5V             Vcc / 5V
    TX1            TX  ( via 1K resistor )
    RX0            RX  ( via 1K resistor )
    D2             RESET


    Publication: Exhibition Gallery / Arduino Forum
    6V6GT  11.09.2017
*/

const byte RST05V = 2 ;              // reset target
const byte dtrDetectPin = A6 ;       // detects DTR on CH340G
const byte serialActivityLed = LED_BUILTIN ;  // replacement for disconnected on board Tx/Rx leds
const byte rxPin = 0 ;
const byte txPin = 1 ;

void setup() {
  // put your setup code here, to run once:
  pinMode(RST05V , OUTPUT);
  digitalWrite( RST05V, HIGH ) ; // pull reset pin HIGH
  pinMode( rxPin , INPUT ) ;
  pinMode( txPin , INPUT ) ;
  pinMode( serialActivityLed , OUTPUT ) ;
}

void loop() {
  // Detects pulse on DTR pin of CH340G and force reset low for X microseconds
  static  int arLast = 0 ;
  static unsigned long resetPulseDetecteAtMs = 0 ;

  static unsigned long lastBlinkAtMs = 0 ;
  static bool blinkPeriod ;
  static bool blinkRequest = false ;
  static byte loopCount = 0 ;
  static bool txStateLast ;
  static bool rxStateLast ;

  unsigned long mS = millis() ;
  int ar = analogRead( dtrDetectPin ) ;


  if ( mS - resetPulseDetecteAtMs > 1000 ) {  // debounce
    if ( arLast - ar > 100  )  {   // falling edge detection
      resetPulseDetecteAtMs = mS ;
      digitalWrite( RST05V, LOW ) ;
      delayMicroseconds( 100 ) ;  // as in ISP sketch
      digitalWrite( RST05V, HIGH ) ;
    }
  }

 
  // flash serial activity - we've lost the leds on the nano.

  bool txState = digitalRead( txPin ) ;
  bool rxState = digitalRead( rxPin ) ;
  if ( txState != txStateLast || rxState != rxStateLast ) blinkRequest = true ;
  txStateLast = txState ;
  rxStateLast = rxState ;


  if ( mS - lastBlinkAtMs > 50 ) {
    lastBlinkAtMs = mS ;
    if ( blinkPeriod && blinkRequest == true ) {
      digitalWrite( serialActivityLed , HIGH ) ;
      blinkRequest = false ;
    }
    else digitalWrite( serialActivityLed , LOW ) ;
    blinkPeriod = ! blinkPeriod ;
  }
  
  arLast = ar ;
}

Go Up