I had the same intermittent code last year. I did same, changed the sensor but code kept coming back. Almost took front engine cover off to check base timing (timing chain). Changed alt & fixed problem.
Alternator when failing can create radio frequencies that are close to the computers & will trigger a false p340 or similar code.