Penicuik is okay by public transport if you're staying in the city centre - there is a good bus service, including night buses. It's next to impossible for me to get to coming from much nearer, though (Midlothian's bus service is organized radially centred on the city).