There will be three all-Premiership ties in the last 16 of the Scottish League Cup.
Rangers face a trip to Kilmarnock, Aberdeen will host newly-promoted St Mirren, and Motherwell will travel to Premiership play-off winners Livingston.
Partick Thistle host Celtic and Hibernian are at home to Ross County in a repeat of the 2016 final.
The ties will be played on the weekend of 18/19 August.
Holders Celtic enter the competition at this stage, along with Aberdeen, Rangers and Hibernian, with all four of Scotland’s European entrants allowed a bye into the second round.
Hearts confirmed their place in the second round with a 5-0 win over Inverness Caledonian Thistle, and now visit Dunfermline Athletic, who beat them at Tynecastle on penalties in the group stage last season.
Championship clubs Ayr United and Queen of the South were both were drawn against top-flight opposition, with Ayr travelling to face Dundee, while Queens host St Johnstone.
The draw in full: Livingston v Motherwell, Dundee v Ayr United, Dunfermline Athletic v Hearts, Kilmarnock v Rangers, Aberdeen v St Mirren, Partick Thistle v Celtic, Hibernian v Ross County, Queen of the South v St Johnstone.